Skip to content

Instantly share code, notes, and snippets.

@flexelem
flexelem / FindNextGreatestGivenArray.java
Last active Aug 29, 2015
Given an array of number find the next greater no in the right of each element.
View FindNextGreatestGivenArray.java
import java.util.Stack;
public class FindNextGreatestGivenArray {
// works in O(n*n)
public int[] findNextGreatestNumbers(int[] numbers) {
if (numbers == null) {
throw new IllegalArgumentException("Given array is null");
}
@flexelem
flexelem / minHeap.java
Created Jun 8, 2014
A min heap implementation in java.
View minHeap.java
import java.util.ArrayList;
public class MinHeap {
private ArrayList<Integer> list;
public MinHeap() {
this.list = new ArrayList<Integer>();
}
@flexelem
flexelem / BinarySearchTree.java
Last active Aug 29, 2015
Binary Search Tree implementation
View BinarySearchTree.java
public class BinarySearchTree {
private Node root;
public BinarySearchTree() {
this.root = null;
}
public BinarySearchTree(Node root) {
@flexelem
flexelem / ComputeSCC.java
Created May 19, 2014
Kosaraju's algorithm to find Strongly Connected Components
View ComputeSCC.java
package compute_strongly_connected_components;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Stack;
public class ComputeSCC {
@flexelem
flexelem / DFSNonRecursiveTest.java
Created May 18, 2014
Non-Recursive DFS implementation by using a Stack
View DFSNonRecursiveTest.java
package graph.search.dfs;
import static org.junit.Assert.assertEquals;
import graph.entity.Vertex;
import java.util.ArrayList;
import java.util.Arrays;
import org.junit.Before;
import org.junit.Test;
@flexelem
flexelem / CountConnectedComponents.java
Created May 11, 2014
Compute the number of connected components of an undirected graph
View CountConnectedComponents.java
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
/**
* This class will compute the number of connected components of an un-directed graph. If graph is fully connected than it will return 1.
*
* @author buraktas
*
*/
@flexelem
flexelem / StackWithTwoQueue.java
Created May 4, 2014
Implement a stack using two queues
View StackWithTwoQueue.java
import java.util.LinkedList;
import java.util.NoSuchElementException;
import java.util.Queue;
/**
* Show how to implement a stack using two queues. Analyze the running time of the queue operations.
*
*/
public class StackWithTwoQueue {
@flexelem
flexelem / QueueWithTwoStack.java
Created May 4, 2014
Queue implementation with using two stacks
View QueueWithTwoStack.java
import java.util.NoSuchElementException;
import java.util.Stack;
/**
* CLRS - Introduction to Algorithms Ex.10.1-6
*
* Show how to implement a queue using two stacks. Analyze the running time of the queue operations
*
* */
public class QueueWithTwoStack {
@flexelem
flexelem / DoubleStack.java
Last active Feb 20, 2020
Two stacks in one array
View DoubleStack.java
import java.util.NoSuchElementException;
/**
* CLRS - Introduction to Algorithms Ex.10.1-2
* Explain how to implement two stacks in one array A[1..n] in such a way that
* neither stack overflows unless the total number of elements in both stacks together is n.
* The PUSH and POP operations should run in O(1) time.
*
* There are two stacks in one array which the first one grows upwards ( A[1..N] ),
* and the second one grows downwards ( A[N..1] ).