Skip to content

Instantly share code, notes, and snippets.

View josephbleau's full-sized avatar

Joey Bleau josephbleau

View GitHub Profile
/*
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
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++;
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
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--;
/*
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}
*/
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;
@josephbleau
josephbleau / canBalance.java
Created November 19, 2013 15:21
Solution to codingbat.com problem: http://codingbat.com/prob/p158767
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];
@josephbleau
josephbleau / maxSpan.java
Created November 19, 2013 01:18
A comprehensive solution to: http://codingbat.com/prob/p189576
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;
@josephbleau
josephbleau / Fix34.java
Last active December 28, 2015 17:49
A comprehensive solution to: http://codingbat.com/prob/p159339 -- This solution also works for the next problem at codingbat, just replace the relevant numbers.
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);
}