-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
35 lines (32 loc) · 925 Bytes
/
index.js
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
35
/**
* @param {number[]} heights
* @return {number}
*/
var largestRectangleArea = function(heights) {
let stack = [];
let result = 0;
heights.push(0);
for (let i = 0; i < heights.length; i++) {
const top_index = stack[stack.length - 1];
if (stack.length === 0 || heights[i] > heights[top_index]) {
stack.push(i);
} else {
const top_index = stack.pop();
result = Math.max(
result,
heights[top_index] * (stack.length === 0 ? i : (i - stack[stack.length - 1] - 1))
);
i--;
}
}
return result;
};
console.log(largestRectangleArea([0,1,0,2,0,3,0]));
console.log(largestRectangleArea([1,2,2]));
module.exports = {
id:'84',
title:'Largest Rectangle in Histogram',
url:'https://leetcode.com/problems/largest-rectangle-in-histogram/',
difficulty:'hard',
have_md:true,
};