Skip to content

Instantly share code, notes, and snippets.

@cschep
Last active August 29, 2015 14:22
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 cschep/6edcd56017b3176f1d2c to your computer and use it in GitHub Desktop.
Save cschep/6edcd56017b3176f1d2c to your computer and use it in GitHub Desktop.
var towerSolver = function(blocks) {
var result = 0;
var wallHeight = 0;
var underWater = [];
for (var i = 0; i < blocks.length; i++) {
if (blocks[i] >= wallHeight || i === blocks.length - 1) {
var smallestWall = Math.min(blocks[i], wallHeight);
wallHeight = blocks[i];
while (underWater.length > 0) {
result += (smallestWall - underWater.pop());
}
} else {
underWater.push(blocks[i]);
}
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment