Skip to content

Commit 05bfa97

Browse files
committed
84
1 parent 8e0b214 commit 05bfa97

File tree

2 files changed

+60
-1
lines changed

2 files changed

+60
-1
lines changed

Day83/index.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/**
2+
* @param {number[]} heights
3+
* @return {number}
4+
*/
5+
var largestRectangleArea = function(h) {
6+
let area = 0;
7+
8+
if(h.length == 1)return h[0];
9+
10+
for(let i=0; i < h.length; i++){
11+
let currH = h[i]
12+
for(let j = i; j < h.length; j++){
13+
currH = Math.min(currH, h[j]);
14+
area = Math.max(area,currH * (j-i+1) )
15+
}
16+
}
17+
18+
return area;
19+
};
20+
21+
22+
23+
/**
24+
* @param {number[]} heights
25+
* @return {number}
26+
*/
27+
var largestRectangleArea = function(heights) {
28+
29+
let area = 0;
30+
let stack = [];
31+
heights.push(0);
32+
heights.unshift(0);
33+
34+
for(let i=0; i < heights.length; i++){
35+
let curr = heights[i];
36+
37+
while(stack && heights[stack[stack.length-1]] > curr){
38+
let j = stack.pop();
39+
let width;
40+
41+
if(stack.length==0){
42+
width =1
43+
}else{
44+
width = i - stack[stack.length-1] -1;
45+
}
46+
47+
area = Math.max(area, width * heights[j])
48+
49+
}
50+
51+
stack.push(i)
52+
53+
}
54+
55+
56+
return area;
57+
};

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,4 +184,6 @@ If you are loving solving problems in leetcode, please contact me to enjoy it to
184184

185185
|Day 81| [754. Reach a Number](https://leetcode.com/problems/reach-a-number/) | [javascript]()|[:memo:](https://leetcode.com/problems/reach-a-number/)|Medium|
186186

187-
|Day 82| [289. Game of Life](https://leetcode.com/problems/game-of-life/) | [javascript]()|[:memo:](https://leetcode.com/problems/game-of-life/)|Medium|
187+
|Day 82| [289. Game of Life](https://leetcode.com/problems/game-of-life/) | [javascript]()|[:memo:](https://leetcode.com/problems/game-of-life/)|Medium|
188+
189+
|Day 83| [84. Largest Rectangle in Histogram](https://leetcode.com/problems/largest-rectangle-in-histogram/) | [javascript]()|[:memo:](https://leetcode.com/problems/largest-rectangle-in-histogram/)|Medium|

0 commit comments

Comments
 (0)