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 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 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 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 { |
NewerOlder