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
/* | |
Given an array of ints, is it possible to choose a group of some of | |
the ints, such that the group sums to the given target? This is a | |
classic backtracking recursion problem. Once you understand the | |
recursive backtracking strategy in this problem, you can use the | |
same pattern for many problems to search a space of choices. | |
Rather than looking at the whole array, our convention is to | |
consider the part of the array starting at index start and continuing | |
to the end of the array. The caller can specify the whole array simply | |
by passing start as 0. No loops are needed -- the recursive calls |
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 countClumps(int[] nums) { | |
int clumps = 0, index = 0; | |
while(index < nums.length-1) { | |
boolean clumpFound = false; | |
while(nums[index] == nums[index+1]) { | |
clumpFound = true; | |
index++; |
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 maxMirror(int[] nums) { | |
if(nums.length == 0) return 0; | |
if(nums.length == 1) return 1; | |
/* From left-to-right, start with character, search from | |
right-to-left for that character, if we find it advance | |
both pointers and compare, if they are different, check | |
this mirror against the last stored mirror size. If the | |
values continue to match, continue matching until | |
the pointers meet and check this value against the |
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[] seriesUp(int n){ | |
int[] a = new int[n*(n+1)/2]; | |
int m = 1; | |
for(int i = 0, j = 0; i < a.length; ++i, ++j) { | |
int inSeq = j%n+1; | |
if(inSeq <= m) | |
a[i] = inSeq; | |
else { | |
j = -1; | |
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
/* | |
Given n>=0, create an array length n*n with the following pattern, | |
shown here for n=3 : {0, 0, 1, 0, 2, 1, 3, 2, 1} | |
(spaces added to show the 3 groups). | |
squareUp(3) → {0, 0, 1, 0, 2, 1, 3, 2, 1} | |
squareUp(2) → {0, 1, 2, 1} | |
squareUp(4) → {0, 0, 0, 1, 0, 0, 2, 1, 0, 3, 2, 1, 4, 3, 2, 1} | |
*/ |
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 boolean linearIn(int[] outer, int[] inner) { | |
if(outer.length == 0) return false; | |
if(inner.length == 0) return true; | |
int i = 0; | |
int j = 0; | |
for(; i < outer.length && j < inner.length; ++i, ++j) { | |
if(inner[j] > outer[i]){ | |
--j; |
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 boolean canBalance(int[] nums) { | |
if(nums.length == 0 || nums.length == 1) return false; | |
int sumOfNums = 0; | |
int leftSum = 0; | |
for(int n : nums){ sumOfNums += n; } | |
for(int i = 0; leftSum < sumOfNums && i < nums.length; ++i){ | |
leftSum += nums[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 maxSpan(int[] nums) { | |
HashMap<Integer, int[]> spanMap = new HashMap(); | |
int max = 0; | |
for(int i = 0; i < nums.length; ++i) { | |
if(!spanMap.containsKey(nums[i])) { | |
spanMap.put(nums[i], new int[]{i,i}); | |
} else { | |
spanMap.get(nums[i])[1] = 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[] fix34(int[] nums) { | |
if(nums.length == 0 || nums.length == 1 ) | |
return nums; | |
ArrayList<Integer> bank = new ArrayList(); | |
for(int i = 0; i < nums.length; ++i) { | |
if(nums[i] == 4){ | |
bank.add(i); | |
} |