Created
January 2, 2020 10:25
-
-
Save kolyuchii/d426f7d5c88138f818c7d9efb1e53502 to your computer and use it in GitHub Desktop.
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
| 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