Skip to content

Instantly share code, notes, and snippets.

@jingz8804
jingz8804 / NaiveUnionFind.java
Last active August 29, 2015 13:56
The naive union find algorithm. It takes O(N) time to union two objects but O(1) to check if two objects are connected.
public class NaiveUnionFind{
private int[] id;
public NaiveUnionFind(int N){
id = new int[N];
for (int i = 0; i < N; i++){
id[i] = i;
}
}
@jingz8804
jingz8804 / WeightedQuickUnionFind.java
Last active May 12, 2021 12:40
Weighted Quick Union Find algorithm (Union-by-Size/Union-by-Height)
public class WeightedQuickUnionFind{
private int[] id;
private int[] sz;
private int[] height; // this is for union-by-height
private int count; // the number of connected components
private int[] maximum; // keep track of the maximum object in each connected component
public WeightedQuickUnionFind(int N){
id = new int[N];
sz = new int[N];
@jingz8804
jingz8804 / SuccessorDelete.java
Created February 17, 2014 18:22
Successor Delete, a very tricky application of Union-Find. How do we relate them together intuitively?
public class SuccessorDelete{
private int[] id;
private int[] sz;
private int[] maximum;
public SuccessorDelete(int N){
id = new int[N];
sz = new int[N];
maximum = new int[N];
for (int i = 0; i < N; i++){
@jingz8804
jingz8804 / ThreeSum.java
Created February 28, 2014 22:32
3-Sum algorithm in O(n^2) time.
import java.util.Hashtable;
public class ThreeSum{
private int[] numbers;
private Hashtable<Integer, Boolean> numTable;
public ThreeSum(int[] numbers){
this.numbers = numbers;
numTable = new Hashtable();
for(int num: numbers){
@jingz8804
jingz8804 / BitonicArraySearch.java
Last active November 29, 2017 07:56
Search an integer in a bitonic array (holding distinct integers)
public class BitonicArraySearch{
private int[] numbers;
private int len;
private int turningPointIndex = -1;
public BitonicArraySearch(int[] numbers){
this.numbers = numbers;
len = numbers.length;
}
@jingz8804
jingz8804 / LinkedListStack.java
Last active August 29, 2015 13:57
A Stack using LinkedList
import java.util.Iterator;
import java.util.NoSuchElementException;
public class LinkedListStack<Item> implements Iterable<Item> {
private class Node {
Item value;
Node next;
}
private Node top;
import java.util.Iterator;
import java.util.NoSuchElementException;
public class ResizingArrayStack<Item> implements Iterable<Item>{
private Item[] values;
private int count;
public ResizingArrayStack(){
values = (Item[]) new Object[1]; // initialize the array to hold 1 element
count = 0;
}
import java.util.NoSuchElementException;
import java.util.Iterator;
public class LinkedListQueue<Item> implements Iterable<Item>{
private class Node{
Item item;
Node next;
}
private Node head; // head of the queue; we dequeue at the head
import java.util.Iterator;
import java.util.NoSuchElementException;
public class ResizingArrayQueue<Item> implements Iterable<Item>{
private Item[] items; // the actual array holding the items
private int head; // the index of the head of the queue
private int tail; // the index of the tail of the queue
private int count; // the number of items in the queue
public ResizingArrayQueue(){
@jingz8804
jingz8804 / Subsets.java
Last active August 29, 2015 13:57
Subsets problem on LeetCode
import java.util.Arrays;
import java.util.ArrayList;
public class Subsets{
public ArrayList<ArrayList<Integer>> subsets(int[] S) {
// sort the array first
Arrays.sort(S);
ArrayList<ArrayList<Integer>> results = new ArrayList<ArrayList<Integer>>();
ArrayList<Integer> emptySet = new ArrayList<Integer>();
results.add(emptySet);