Last active
August 29, 2015 14:22
-
-
Save cschep/6edcd56017b3176f1d2c to your computer and use it in GitHub Desktop.
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
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