Created
May 26, 2016 05:25
-
-
Save linfongi/ab78bb58ee797b9c09344a48688223f0 to your computer and use it in GitHub Desktop.
This file contains 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
/** | |
* @param {number[]} heights | |
* @return {number} | |
*/ | |
function largestRectangleArea(heights) { | |
heights.push(-1); | |
var stack = []; | |
var max = 0; | |
heights.forEach((h, i) => { | |
if (!stack.length || heights[stack.slice(-1)[0]] < h) { | |
return stack.push(i); | |
} | |
while (stack.length && heights[stack.slice(-1)[0]] >= h) { | |
var height = heights[stack.pop()]; | |
var left = stack.length ? stack.slice(-1)[0] + 1 : 0; | |
max = Math.max(max, height * (i - 1 - left + 1)); | |
} | |
stack.push(i); | |
}); | |
return max; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment