Skip to content

Instantly share code, notes, and snippets.

@msimpson
Created October 30, 2013 11:25
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 msimpson/eb0189dd687d696ab70a to your computer and use it in GitHub Desktop.
Save msimpson/eb0189dd687d696ab70a to your computer and use it in GitHub Desktop.
var arr = [2,5,1,3,1,2,1,7,7,6,5,9,0,1,0,0,20,18,5,20,0,1,0,1];
function getPuddleVolume(arr) {
var max = Math.max.apply(null, arr),
volume = 0;
for (var y = 1; y <= max; y++) {
var inside = false,
gaps = 0;
for (var x = 0; x < arr.length; x++) {
if (arr[x] >= y) {
if (inside) {
inside = false;
volume += gaps;
x--;
} else {
inside = true;
gaps = 0;
}
} else {
gaps++;
}
}
}
return volume;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment