Skip to content

Instantly share code, notes, and snippets.

public abstract class AbstractTreeIterator<E> implements Iterator<E> {
protected AbstractTreeIterator(TreeNode<E> root){
assert(root!=null);
}
@Override
public void remove() {
throw new UnsupportedOperationException("remove is not supported");
}
@deyindra
deyindra / LRU Cache
Created October 13, 2018 18:07
LRU Cache
package org.idey.algo.datastructure.cache;
import java.util.HashMap;
import java.util.Map;
public class LRUCache<K,V> {
private int capacity;
private Map<K, Node<K,V>> map = new HashMap<>();
private DoubleLinkedList<K,V> doubleLinkedList=new DoubleLinkedList<>();
@deyindra
deyindra / LFUCache
Last active October 13, 2018 18:07
LFU Cache
package org.idey.algo.datastructure.cache;
import java.util.HashMap;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;
public class LFUCache<K,V> {
private Map<K, Node<K,V>> map = new HashMap<>();
private Map<K, Integer> counts = new HashMap<>();
@deyindra
deyindra / AssertJ
Created October 9, 2018 21:31
User Hit count
public class AssertJ {
private AssertJ(){
throw new AssertionError("Invalid Access");
}
/**
*
* @param object which will satisfy following code
* <pre>
* new Predicate&lt;T&gt;(){
@deyindra
deyindra / FindPair
Created September 17, 2018 19:50
Find Pair
public static void printPairUnsorted(int[] array, int sum){
Set<Integer> s = new HashSet<>();
for(int v:array){
int temp = sum - v;
if(s.contains(temp)){
System.out.println(String.format("Pair found %d %d", temp,v));
}
s.add(v);
}
}
@deyindra
deyindra / Reverse
Created September 16, 2018 20:20
Reverse Word
public static String reverseWord(String str){
char[] array = str.toCharArray();
int start=0;
int end=array.length-1;
reverse(array,0,array.length-1);
while (start<=end && array[start]==' ')
start++;
while (end>=start && array[end] == ' ')
end--;
@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;
@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 / 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 / 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