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 FlattentNestedIterator<T> implements Iterator<T>{ | |
private Stack<Iterator<Nested<T>>> stack = new Stack<>(); | |
private T current; | |
private boolean hasNext; | |
public FlattentNestedIterator(Iterator<Nested<T>> nestedIterator) { | |
if(nestedIterator == null){ | |
throw new IllegalArgumentException("Invalid Iterator"); | |
} | |
stack.push(nestedIterator); | |
advance(); |
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.List; | |
import java.util.Objects; | |
abstract class AbstractListCommand<T> { | |
protected final String opName; | |
protected final int index; | |
protected T newObject; | |
protected T oldObject; | |
protected AbstractListCommand(final int index, final String opName) { |
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 org.idey.algo.datastructure; | |
import java.util.*; | |
public class Trie<T> { | |
private TrieNode<T> root; | |
public Trie() { | |
this.root = new TrieNode<>(); | |
} | |
public void insert(String word, T object){ |
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 TreeNode<E> { | |
private E data; | |
private TreeNode<E> left; | |
private TreeNode<E> right; | |
public TreeNode(E data) { | |
this.data = data; | |
} | |
public void setLeft(TreeNode<E> left) { |
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(1) | |
public static List<Pair<Integer, Integer>> getPairs(int[] array, final int sum){ | |
int start = 0; | |
int end = array.length-1; | |
List<Pair<Integer, Integer>> list = new ArrayList<>(); | |
while (start < end){ | |
int resultSum = array[start] + array[end]; | |
if(resultSum < sum){ | |
start++; | |
}else if (resultSum > sum){ |
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 O(n) and Space O(longN) | |
public static List<Pair<Integer, Integer>> getPairs(final TreeNode<Integer> tree, final int sum){ | |
List<Pair<Integer, Integer>> list = new ArrayList<>(); | |
Stack<TreeNode<Integer>> left = new Stack<>(); | |
Stack<TreeNode<Integer>> right = new Stack<>(); | |
TreeNode<Integer> leftCurrent = tree; | |
TreeNode<Integer> rightCurrent = tree; | |
while (!left.isEmpty() || !right.isEmpty() || leftCurrent!=null || rightCurrent!=null){ |
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 language.implicitConversions | |
import language.reflectiveCalls | |
/** | |
* This Objects provide safe conversion of [[String]] value to [[Any]] specific type, and return [[Option]] | |
* of the same type after conversion. In case of any exception it will return `None` | |
* Current conversion for following types are supported | |
* `Int`, `Float`, `Double`, `Boolean`, `Short`, `Byte`, `Long`. For any custom type one need to create an | |
* `implicit object` extending `StringConverter` trait | |
* For example |
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.*; | |
public class Anagrams { | |
private static int[] primeTables; | |
private static void populatePrimeTable(){ | |
if(primeTables==null){ | |
primeTables = new int[128]; | |
PrimeNumber primeNumber = new PrimeNumber(10000); |
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.ArrayList; | |
import java.util.Collections; | |
import java.util.List; | |
public class WaterBucket { | |
private static int gcd(int a, int b){ | |
if(a==0) | |
return b; | |
return gcd(b%a, a); | |
} |
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 static double findMedian(int[] input1, int[] input2){ | |
if(input1.length>input2.length){ | |
return findMedian(input2,input1); | |
} | |
int x = input1.length; | |
int y = input2.length; | |
int low = 0; | |
int high = x; |