Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | 11x 11x 11x 11x 11x 11x 46x 24x 24x 46x 11x | export default function largestRectangleArea(heights: number[]): number {
heights.unshift(0)
heights.push(0)
let res = 0
// Record indices of elements with increase height
const increaseStack = []
increaseStack.push(0)
for (let i = 1, top = 0; i < heights.length; i++) {
while (
// eslint-disable-next-line no-cond-assign -- Correctly assigns `top`.
heights[i] < heights[(top = increaseStack[increaseStack.length - 1])]
) {
increaseStack.pop()
res = Math.max(
res,
heights[top] * (i - increaseStack[increaseStack.length - 1] - 1),
)
}
// heights[i] >= heights[increaseStack.top()]
// Push index of higher element to stack
increaseStack.push(i)
}
return res
}
|