Skip to content

Instantly share code, notes, and snippets.

@bradclawsie
Created December 20, 2013 07:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bradclawsie/8051642 to your computer and use it in GitHub Desktop.
Save bradclawsie/8051642 to your computer and use it in GitHub Desktop.
package org.b7j0c.puzzles;
// http://programmingpraxis.com/2013/11/15/twitter-puddle/
public final class Puddles {
public static void main(String[] args) {
int[] c = {2,5,1,2,3,4,7,7,6,1,1,9,8,8,9,1,1};
int cl = c.length;
int w = -1;
int sub = 0;
for (int i=1; i < cl; i++) {
if (c[i] > c[i-1]) { // i is a right wall
if (w != -1) {
sub += c[i-1];
if (c[i] >= c[w]) {
// vol is height of left wall w * dist to i (right wall)
// minus accumulated columns heights in valley
int vol = (c[w] * (i-w-1)) - sub;
System.out.printf("from %d to %d, vol %d\n",w,i,vol);
sub = 0;
w = -1;
}
}
} else if (c[i] < c[i-1]) { // i-1 is a left wall
w = i-1;
} else { // if there is a left wall, add this to valley column heights
if (w >= 0) sub += c[i-1];
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment