Last active
December 31, 2015 19:39
-
-
Save allumbra/8035125 to your computer and use it in GitHub Desktop.
Twitter interview question
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
function mySolution(walls){ | |
var heightLoc = {}; | |
var waterSum = 0; | |
for(var x=0; x<walls.length; x++){ | |
for(var y=0; y<walls[x]; y++){ | |
var height = y+''; | |
waterSum += height in heightLoc ? (x - heightLoc[height]) - 1 : 0; | |
heightLoc[height] = x; // record last time we saw a wall of this height | |
} | |
} | |
return waterSum; | |
} | |
var walls = [2,5,1,3,1,2,1,7,1,5,1]; | |
var myResult = mySolution(walls); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment