2
2
* @param {number[] } heights
3
3
* @return {number }
4
4
*/
5
- var largestRectangleArea = function ( h ) {
5
+ var largestRectangleArea = function ( h ) {
6
6
let area = 0 ;
7
7
8
- if ( h . length == 1 ) return h [ 0 ] ;
8
+ if ( h . length == 1 ) return h [ 0 ] ;
9
9
10
- for ( let i = 0 ; i < h . length ; i ++ ) {
10
+ for ( let i = 0 ; i < h . length ; i ++ ) {
11
11
let currH = h [ i ]
12
- for ( let j = i ; j < h . length ; j ++ ) {
12
+ for ( let j = i ; j < h . length ; j ++ ) {
13
13
currH = Math . min ( currH , h [ j ] ) ;
14
- area = Math . max ( area , currH * ( j - i + 1 ) )
14
+ area = Math . max ( area , currH * ( j - i + 1 ) )
15
15
}
16
16
}
17
17
@@ -24,24 +24,24 @@ var largestRectangleArea = function(h) {
24
24
* @param {number[] } heights
25
25
* @return {number }
26
26
*/
27
- var largestRectangleArea = function ( heights ) {
27
+ var largestRectangleArea = function ( heights ) {
28
28
29
29
let area = 0 ;
30
30
let stack = [ ] ;
31
31
heights . push ( 0 ) ;
32
32
heights . unshift ( 0 ) ;
33
33
34
- for ( let i = 0 ; i < heights . length ; i ++ ) {
34
+ for ( let i = 0 ; i < heights . length ; i ++ ) {
35
35
let curr = heights [ i ] ;
36
36
37
- while ( stack && heights [ stack [ stack . length - 1 ] ] > curr ) {
37
+ while ( stack && heights [ stack [ stack . length - 1 ] ] > curr ) {
38
38
let j = stack . pop ( ) ;
39
39
let width ;
40
40
41
- if ( stack . length == 0 ) {
42
- width = 1
43
- } else {
44
- width = i - stack [ stack . length - 1 ] - 1 ;
41
+ if ( stack . length == 0 ) {
42
+ width = 1
43
+ } else {
44
+ width = i - stack [ stack . length - 1 ] - 1 ;
45
45
}
46
46
47
47
area = Math . max ( area , width * heights [ j ] )
0 commit comments