Created
June 28, 2012 16:12
-
-
Save OpenGrid/3012226 to your computer and use it in GitHub Desktop.
Sigma 2012 Codility Programming Certificate Solution
This file contains hidden or 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
/* http://blog.codility.com/2012/06/sigma-2012-codility-programming.html */ | |
function stone_wall ( H ) { | |
var blocks = 0, stack = [], m, stack_length = 0; | |
for(m in H) { | |
// I use stack to remember all previous skyline levels | |
while(stack_length > 0 && stack[stack_length - 1] > H[m]) { | |
stack_length -= 1; | |
blocks += 1; | |
} | |
if(stack_length === 0 || stack[stack_length - 1] < H[m]) { | |
stack[stack_length] = H[m]; | |
stack_length += 1; | |
} | |
} | |
return blocks + stack_length; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment