Skip to content

Instantly share code, notes, and snippets.

View alexandervasyuk's full-sized avatar

alexandervasyuk

View GitHub Profile
@alexandervasyuk
alexandervasyuk / gist:fb24a5aa16bfee1a9755
Created September 26, 2014 18:10
Partition linked list around x
public Node partition(Node node, int x) {
Node lessHead, lessTail, greaterHead, greaterTail;
while (node != null) {
Node next = node.next;
node.next = null;
if (node.data < x) {
if (lessHead == null) {
lessHead = lessTail = node;
} else {
@alexandervasyuk
alexandervasyuk / pairsThatSumUpToK
Last active August 29, 2015 14:07
Given an integer array, output all pairs that sum up to a value k
public static List<Pair> pairsThatSumUpToK(int[] a, int k) {
Arrays.sort(a);
List<Pair> result = new ArrayList<Pair>();
int left = 0;
int right = a.length-1;
while(left < right) {
int sum = a[left] + a[right];
if (sum < k) left++;
else if (sum > k) right--;
@alexandervasyuk
alexandervasyuk / pairsThatSumUpToKOptimized
Created October 1, 2014 23:59
pairsThatSumUpToK Optimized
public static List<Pair> pairsThatSumUpToK(int[] a, int k) {
HashSet<Integer> set = new HashSet<Integer>();
List<Pair> result = new ArrayList<Pair>();
for (int i = 0 ; i < a.length; i++) {
int lookup = k - i;
if (set.contains(lookup)) {
result.add(new Pair(lookup, i));
} else {
set.add(i);
}
@alexandervasyuk
alexandervasyuk / matrixRegionSumNaive
Created October 2, 2014 17:50
MatrixRegionSumNaive
public int matrixRegionSumNaive(int[][] matrix, Coordinate A, Coordinate D) {
int result = 0;
for (int j = A.y; j < matrix.length; j++) {
for (int i = A.x; i < matrix[0].length; i++) {
result += matrix[i][j];
}
}
return result;
}
@alexandervasyuk
alexandervasyuk / matrixRegionSum
Created October 2, 2014 22:23
matrixRegionSum
public class SummableMatrix {
private HashMap<Coordinate, Integer> cache;
public SummableMatrix(int[][] matrix) {
cache = preprocess(matrix);
}
public static HashMap<Coordinate, Integer> preprocess(int[][] matrix) {
HashMap<Coordinate, Integer> result = new HashMap<Coordinate, Integer>();
@alexandervasyuk
alexandervasyuk / largestContinuousSum
Last active August 29, 2015 14:07
largestContinuousSum
public static LinkedList<Integer> largestContinuousSum(int[] array) {
if (array.length == 0) return null;
LinkedList<Integer> current_path, max_path;
int current_sum, max_sum;
max_path = current_path = new LinkedList<Integer>();
current_sum = max_sum = Integer.MIN_VALUE;
@alexandervasyuk
alexandervasyuk / merge
Created October 4, 2014 04:30
Merge Two Objects
/*
Write a function that will recursively merge two objects with the following conditions:
1.) If a[field] is an array, and b[field] is defined and is not an array, add b[field] to the array
2.) If a[field] is an array an b[field] exists but is undefined or null, set a[field] to an empty array
3.) If a[filed] is an array and b[field] is an array, set a[field] to b[field]
4.) If a[field] exists and b[field] exists but is undefined, delete a[field]
5.) If b[field] is a non-complex type (number, string, boolean, et cetera), copy to a[field]
*/
var a = {
@alexandervasyuk
alexandervasyuk / deepCompare
Created October 4, 2014 20:57
Deep Compare
var deepCompare = function(a,b) {
for (var field in b) {
if (a.hasOwnProperty(field)){
if (a[field] instanceof Object) {
return deepCompare(a[field], b[field]);
} else if (a[field] !== b[field]){
return false
}
} else {
return false;
@alexandervasyuk
alexandervasyuk / simpleShuntingYardPseudo
Last active August 29, 2015 14:07
simpleShuntingYardPseudo
While there are tokens to be read:
Read a token.
If the token is a number, then add it to the output queue.
If the token is an operator, o1, then:
while there is an operator token, o2, at the top of the operator stack, and
o1 has precedence less than or equal to that of o2,
pop o2 off the stack, onto the output queue;
push o1 onto the stack.
When there are no more tokens to read:
While there are still operator tokens in the stack:
@alexandervasyuk
alexandervasyuk / simpleinfixToBinaryTreePseudocode
Last active August 29, 2015 14:07
Simple infixToBinaryTree Pseudocode
Declare BinaryTreeNode result;
While there are tokens to be read:
Read a token.
If the token is a number, then add it to the output stack.
If the token is an operator, o1, then:
while there is an operator token, o2, at the top of the operator stack, and
o1 has precedence less than or equal to that of o2,
operator = pop o2 off the operator stack;
if result is null:
set result's data to operator