Skip to content

Instantly share code, notes, and snippets.

View junminstorage's full-sized avatar

Junmin Liu junminstorage

View GitHub Profile
@junminstorage
junminstorage / gist:4b08142954a824344072
Last active August 29, 2015 14:18
Int to Int hash Map implementation using linear probling
package org.blueocean.hash;
import java.util.Arrays;
import java.util.Random;
/*
* an implementation of int to int hash based on open addressing linear probing approach
*
* only int[] array is maintained, <k, v> is inserted using a serial of steps and
* based on a hash function hash(k, step) to find a open slot
@junminstorage
junminstorage / gist:b01d06d3ee0a706f9da9
Created May 12, 2015 20:27
merge sort of linked list
public class SortQ {
public static class Node {
int d;
Node next;
}
public static Node merge_sort2(Node head){
assert(head!=null);
@junminstorage
junminstorage / gist:c540070ad36f6ecc322b
Created May 22, 2015 19:37
Rabin Karp Algorithm - string rolling hash
package org.blueocean;
public class RabinKarpString {
static final int prime = 101;
static final int base = 103;
public static void rabinKarp(String s){
assert(s!=null && !s.isEmpty());
long leftHash = s.charAt(0);
long rightHash = s.charAt(0);
public static void rotateMatrixByElement(int[][] matrix){
int num = matrix.length;
for(int i=0; i<num/2; i++){
int start = matrix[i][i];
//shift up on column i
for(int row=i; row<num-i-1; row++){
matrix[row][i] = matrix[row+1][i];
}
//shift left on row num-i-1
for(int col=i; col<num-i-1; col++){
@junminstorage
junminstorage / findContinuous1sRec
Last active November 21, 2015 16:36
Given a array consisted of only 0 or 1, and a number N. We have N times to flip 0 to 1 in array. Find the longest continuous 1 after we flipped the N zero elements. Rotation is NOT allowed. For example, 100100111000, n=2, the best flip is 100111111000, return 6 10001101101, n=2, the best flip is 10001111111, return 7
public static int findContinuous1s(String s, int maxFlips){
int[] max = new int[1];
findContinuous1sRec(s, 0, 0, maxFlips, 0, max);
return max[0];
}
public static void findContinuous1sRec(String s, int start, int current, int maxFlips, int flips, int[] max){
//keep on eating up 1s if we can
while(current<s.length() && s.charAt(current) == '1')
current++;
@junminstorage
junminstorage / findContinuous1sWithRotation
Created November 21, 2015 17:25
Given a array consisted of only 0 or 1, and a number N. We have N times to flip 0 to 1 in array. Find the longest continuous 1 after we flipped the N zero elements. Rotation is allowed. For example, 100100111000, n=2, the best flip is 100111111000, return 6 10001101101, n=2, the best flip is 10001111111, return 8(rotation is allowed)
public static int findContinuous1s(String s, int maxFlips){
int[] max = new int[1];
findContinuous1sRec(s, 0, 0, maxFlips, 0, max);
return max[0];
}
public static void findContinuous1sRec(String s, int start, int current, int maxFlips, int flips, int[] max){
//keep on eating up 1s if we can
while(current<s.length() && s.charAt(current) == '1')
current++;
@junminstorage
junminstorage / gist:ee07e8e6e260cc1ca2ed
Created December 3, 2015 03:52
min path sum for number triangle
public static int minPath(List<int[]> input){
int levels = input.size();
int[][] dp = new int[levels][levels];
//we start at the bottom
//here the min path sum is the numbers at the bottom
dp[levels-1] = input.get(levels-1);
//now we go up
for(int l=levels-2; l>=0; l--){
@junminstorage
junminstorage / minpath.java
Created December 3, 2015 03:54
min path for number triangle
public static int minPathBetter(List<int[]> input){
int levels = input.size();
int[] dp = new int[levels];
//we start at the bottom
//here the min path sum is the numbers at the bottom
dp = input.get(levels-1);
//now we go up
for(int l=levels-2; l>=0; l--){
for(int pos = 0; pos<=l; pos++){
dp[pos] = Math.min(dp[pos], dp[pos+1]) + input.get(l)[pos];
@junminstorage
junminstorage / sumofSubset
Created December 3, 2015 18:24
Given a integer array, and a number n. Return true if n can be consisted by a subset of array. For example, arr[]={3, 2, 3, 5}, n=11, return true; arr[]={3, 2, 3, 5}, n=12, return false
/*
* the algorithm will generate sums for all possible subset of the array
* and put them into a hash, return true if the target is found in the hash
* the optimization here is to check hash contains the target during the update
*/
public static boolean sumOfSubSet(int[] nums, int target){
Set<Integer> sums = new HashSet<Integer>();
//start with empty set
sums.add(0);
if(sums.contains(target))
@junminstorage
junminstorage / minimumSizeArray.java
Last active December 5, 2015 01:41
Given an array of n positive integers and a positive integer s, find the minimal length of a subarray of which the sum ≥ s. If there isn't one, return 0 instead. For example, given the array [2,3,1,2,4,3] and s = 7, the subarray [4,3] has the minimal length of 2 under the problem constraint.
public static int minimumSizeArray(int[] nums, int s){
int min = Integer.MAX_VALUE, sum = 0;
int p1=0,p2=0,len=nums.length;
while(p2<len && p1<len){
sum += nums[p2];
while(p1<len && sum >=s){
min = Math.min(p2-p1+1, min);
sum -= nums[p1];
p1++;