File tree Expand file tree Collapse file tree 1 file changed +30
-0
lines changed Expand file tree Collapse file tree 1 file changed +30
-0
lines changed Original file line number Diff line number Diff line change
1
+ /*
2
+ * @lc app=leetcode.cn id=84 lang=javascript
3
+ *
4
+ * [84] 柱状图中最大的矩形
5
+ */
6
+
7
+ // @lc code=start
8
+ /**
9
+ * @param {number[] } heights
10
+ * @return {number }
11
+ */
12
+ var largestRectangleArea = function ( heights ) {
13
+ let result = 0 ;
14
+ const stack = [ - 1 ] ;
15
+ for ( let i = 0 ; i < heights . length ; i ++ ) {
16
+ while ( stack [ stack . length - 1 ] !== - 1 && heights [ stack [ stack . length - 1 ] ] >= heights [ i ] ) {
17
+ const j = stack . pop ( ) ;
18
+ result = Math . max ( result , heights [ j ] * ( i - stack [ stack . length - 1 ] - 1 ) ) ;
19
+ }
20
+ stack . push ( i ) ;
21
+ }
22
+
23
+ while ( stack [ stack . length - 1 ] !== - 1 ) {
24
+ const j = stack . pop ( ) ;
25
+ result = Math . max ( result , heights [ j ] * ( heights . length - stack [ stack . length - 1 ] - 1 ) ) ;
26
+ }
27
+
28
+ return result ;
29
+ } ;
30
+ // @lc code=end
You can’t perform that action at this time.
0 commit comments