Skip to content

Instantly share code, notes, and snippets.

@deyindra
deyindra / FlattentNestedIterator
Created May 15, 2017 21:42
Unfolded Iterator
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();
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) {
@deyindra
deyindra / Trie
Last active July 12, 2017 02:11
Phone Directory Lookup
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){
@deyindra
deyindra / NthSmallestOrLargest
Created August 24, 2017 00:30
Get Nth Smallest or Largest Element from BST (Binary Search Tree)
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) {
@deyindra
deyindra / FindPair
Last active August 29, 2017 21:46
Find pair from Sorted Array
//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){
@deyindra
deyindra / PairFromBST
Created August 29, 2017 21:46
TreeNode
//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){
@deyindra
deyindra / Scala Implicit
Last active December 3, 2017 22:06
Scala Implicit
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
@deyindra
deyindra / Group Anagram (Prime Hash)
Created August 8, 2018 05:50
Group Anagram (Prime Hash)
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);
@deyindra
deyindra / WaterJug
Created August 16, 2018 15:17
Water Jug problem with minimum set of steps
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);
}
@deyindra
deyindra / Find median
Last active September 16, 2018 02:52
Median of 2 sorted array of different length
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;