This file contains 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 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 { |
This file contains 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 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--; |
This file contains 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 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); | |
} |
This file contains 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 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; | |
} |
This file contains 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 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>(); |
This file contains 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 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; | |
This file contains 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
/* | |
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 = { |
This file contains 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
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; |
This file contains 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
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: |
This file contains 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
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 |
OlderNewer