Skip to content

Instantly share code, notes, and snippets.

@allumbra
Last active December 31, 2015 19:39
Show Gist options
  • Save allumbra/8035125 to your computer and use it in GitHub Desktop.
Save allumbra/8035125 to your computer and use it in GitHub Desktop.
Twitter interview question
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