Skip to content

Instantly share code, notes, and snippets.

@antonybudianto
Last active July 30, 2017 10:10
Show Gist options
  • Save antonybudianto/dc63c728dd3ea9e26d5ee4c881b6edb6 to your computer and use it in GitHub Desktop.
Save antonybudianto/dc63c728dd3ea9e26d5ee4c881b6edb6 to your computer and use it in GitHub Desktop.
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(H) {
// write your code in JavaScript (Node.js 6.4.0)
let len = H.length;
if (len === 1) return 1;
let low = 0;
let stack = [];
let tmp = 0, count=0, countB=0;
for(let i=0;i<len;i++) {
let h = H[i];
if (stack.length === 0) {
tmp = h;
stack.unshift(h); continue;
}
if (stack[0] !== h) {
// console.log('h:'+h, 'tmp:'+tmp, h - tmp >= 0 ? 'muat' : 'gk muat')
if (h - tmp >= 0) {
tmp = h
stack.unshift(h);
count++;
} else {
if (count > 0) count--
countB++;
tmp = tmp + h-tmp
stack.unshift(h);
}
} else {
tmp -= h
}
}
// console.log(stack, stack.length, count)
return stack.length - count;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment