Skip to content

Instantly share code, notes, and snippets.

@OpenGrid
Created June 28, 2012 16:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save OpenGrid/3012226 to your computer and use it in GitHub Desktop.
Save OpenGrid/3012226 to your computer and use it in GitHub Desktop.
Sigma 2012 Codility Programming Certificate Solution
/* 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