Skip to content

Instantly share code, notes, and snippets.

View shaaslam's full-sized avatar

Shahrukh Aslam shaaslam

View GitHub Profile
@shaaslam
shaaslam / Histogram3.java
Created February 6, 2018 19:19
Approach #3 (Divide and Conquer Approach)
public class Histogram3 {
public static int largestRectangleArea(int[] height, int start, int end) {
if(start > end) return 0;
int minIndex = start;
for(int i=start; i < end; i++) {
if(height[i] < height[minIndex]) minIndex = i;
}
int maxArea = height[minIndex] * (end - start + 1);
int left = Math.max(maxArea, largestRectangleArea(height, start, minIndex-1));
@shaaslam
shaaslam / InorderIterative.java
Created December 14, 2017 19:18
Given a binary tree, return the inorder traversal of its nodes' values.
/*
Time complexity : O(n).
Space complexity : O(n).
*/
class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> list = new ArrayList<Integer>();
Stack<TreeNode> stack = new Stack<TreeNode>();
TreeNode curr = root;
while(curr != null || !stack.isEmpty()){
@shaaslam
shaaslam / BSTIterator.java
Created December 14, 2017 04:22
Implement an iterator over a binary search tree (BST).
package trees;
import java.util.Stack;
/*
Implement an iterator over a binary search tree (BST).
Your iterator will be initialized with the root node of a BST.
Calling next() will return the next smallest number in the BST.
Note: next() and hasNext() should run in average O(1) time and uses O(h) memory,
where h is the height of the tree.
https://discuss.leetcode.com/topic/6575/my-solutions-in-3-languages-with-stack
@shaaslam
shaaslam / WildCard.java
Last active February 12, 2018 23:15
WildCard.java
/*
* Q3:
Input: 10?
Output: 101, 100
i.e. ? behaves like a wild-card. There are two possibilities for 10?, when that ? is replaced with
either 0 or 1.
Input: 1?0?
Output: 1000, 1001, 1100, 1101
Please write a program that takes given strings as input and produces the suggested output.
Suggested time: 20 minutes.
@shaaslam
shaaslam / setup.sh
Last active November 27, 2017 21:48
#!/bin/bash
##
## FILE: setup.sh
##
## DESCRIPTION: Script to generate the sample files for downloading rest api
##
## AUTHOR: SHAHRUKH ASLAM
##
## DATE: 11-27-2017
##
import java.util.Arrays;
public class Successor {
public static void main(String[] args) {
int array[] = {30, 20, 54, 85, 10, 46, 5, 13, 26};
Arrays.sort(array);
TreeNode node = TreeNode.createMinimalBST(array);
node.print();
import java.util.HashMap;
public class PhoneDialer {
public static HashMap<String, String> map = new HashMap<>();
static {
map.put("0", "0");
map.put("1", "1");
map.put("2", "ABC");
map.put("3", "DEF");
public class Flip {
public static void main(String[] args) {
int array[] = {1,2,3,4,5,6,7};
TreeNode node = TreeNode.createMinimalBST(array);
node.print();
flip(node);
node.print();
}
/*
* Let T be a rooted tree. The lowest common ancestor between two nodes n1 and n2 is defined
as the lowest node in T that has both n1 and n2 as descendants.
The LCA of n1 and n2 in T is the shared ancestor of n1 and n2 that is located farthest from the
root. Computation of lowest common ancestors may be useful, for instance, as part of a
procedure for determining the distance between pairs of nodes in a tree: the distance from n1
to n2 can be computed as the distance from the root to n1, plus the distance from the root to
n2, minus twice the distance from the root to their lowest common ancestor. (Source: Wikipedia)
Design an write an algorithm to find the LCA node, given two nodes in a Binary Tree.
package tree;
import java.util.ArrayList;
import java.util.Arrays;
public class PrintAllPaths {
public static void main(String[] args) {
int array[] = {30, 20, 54, 85, 10, 46, 5, 13, 26};
Arrays.sort(array);