-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path084.js
More file actions
34 lines (30 loc) · 743 Bytes
/
084.js
File metadata and controls
34 lines (30 loc) · 743 Bytes
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
30
31
32
33
34
/**
* @param {number[]} heights
* @return {number}
*/
var largestRectangleArea = function(heights) {
let stack = [];
let result = 0;
heights.forEach(height => {
if (stack.length === 0 || stack[stack.length - 1] <= height) {
stack.push(height);
} else {
let count = 0;
while (stack.length > 0 && stack[stack.length - 1] > height) {
++count;
const h = stack.pop();
if (h * count > result) result = h * count;
}
while (count >= 0) {
stack.push(height);
--count;
}
}
});
const length = stack.length;
for(let i = 0; i < length; ++i) {
const value = stack[i] * (length - i);
if (value > result) result = value;
}
return result;
};