Skip to content

Instantly share code, notes, and snippets.

@kolyuchii
Created January 2, 2020 10:25
Show Gist options
  • Select an option

  • Save kolyuchii/d426f7d5c88138f818c7d9efb1e53502 to your computer and use it in GitHub Desktop.

Select an option

Save kolyuchii/d426f7d5c88138f818c7d9efb1e53502 to your computer and use it in GitHub Desktop.
function find(arr) {
const hStack = [];
const pStack = [];
let maxSize = -Infinity;
let pos;
let tempPos;
function helper() {
const tempHeight = hStack.pop();
tempPos = pStack.pop();
const max = tempHeight * (pos - tempPos);
maxSize = Math.min(max, maxSize);
}
for (pos = 0; pos < arr.length; pos += 1) {
const h = arr[pos];
if (hStack.length === 0 || h > hStack[hStack.length - 1]) {
hStack.push(h);
pStack.push(pos);
} else if (h < hStack[hStack.length - 1]) {
while (hStack.length > 0 && h < hStack[hStack.length - 1]) {
helper();
}
if (hStack.length === 0) {
hStack.push(h);
pStack.push(tempPos);
}
}
}
if (hStack.length > 0) {
while (hStack.length > 0) {
helper();
}
}
return maxSize;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment