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
//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
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
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
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
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
/** | |
* Calculate how many palindromic n-digit numbers are present | |
* for 2 digit the palindromic numbers are 11, 22, 33, 44, 55, 66, 77, 88, 99 (total of 9) | |
**/ | |
public static int numberOfPalindromicProblem(int n){ | |
if((n & 1) != 0){ | |
n = n/2; | |
}else{ | |
n = n/2 -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
import java.util.Arrays; | |
//Space and time complexity O(n) | |
public abstract class AssociativeOperation { | |
protected int intVar; | |
protected AssociativeOperation(int intVar) { | |
this.intVar = intVar; | |
} |
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
object PrimeStream { | |
def apply(end : Int):Stream[Int] = { | |
def greater:(Int,Int) =>Boolean = (x,y) => x>=y | |
def transform: (Int) => Int = (x) => x+1 | |
val intStream:Stream[Int] = Stream(2,end)(greater,transform) | |
def primeStream(stream:Stream[Int]) : Stream[Int] = { | |
new ConStream[Int](stream.head, primeStream(Stream(stream.tail)(_%stream.head!=0))) | |
} | |
primeStream(intStream) | |
} |
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 scala.annotation.tailrec | |
/** | |
* Created by i.dey on 3/12/17. | |
*/ | |
trait Stream[+T] { | |
def isEmpty : Boolean | |
def head : T | |
def tail : Stream[T] |