This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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()){ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
## | |
## FILE: setup.sh | |
## | |
## DESCRIPTION: Script to generate the sample files for downloading rest api | |
## | |
## AUTHOR: SHAHRUKH ASLAM | |
## | |
## DATE: 11-27-2017 | |
## |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |