File tree 1 file changed +17
-25
lines changed
1 file changed +17
-25
lines changed Original file line number Diff line number Diff line change @@ -27,38 +27,30 @@ function readLine() {
27
27
// Complete the largestRectangle function below.
28
28
function largestRectangle ( h ) {
29
29
let max = Number . MIN_VALUE ;
30
- for ( var i = 0 ; i < h . length ; i ++ ) {
31
- let l = i - 1 ;
32
- let r = i + 1 ;
33
- let m = 1 ;
34
- while ( l > - 1 ) {
35
- if ( h [ l ] >= h [ i ] ) {
36
- // console.log("left", l)
37
- m ++ ;
38
- }
39
- else {
30
+ for ( var ith = 0 ; ith < h . length ; ith ++ ) {
31
+ let left = ith - 1 ;
32
+ let right = ith + 1 ;
33
+ let buildings = 1 ;
34
+ while ( left > - 1 ) {
35
+ if ( h [ left ] >= h [ ith ] )
36
+ buildings ++ ;
37
+ else
40
38
break ;
41
- }
42
- l -- ;
39
+ left -- ;
43
40
}
44
- while ( h . length > r ) {
45
- // console.log("right", r)
46
- if ( h [ r ] >= h [ i ] ) {
47
- m ++ ;
48
- }
49
- else {
41
+ while ( h . length > right ) {
42
+ if ( h [ right ] >= h [ ith ] )
43
+ buildings ++ ;
44
+ else
50
45
break ;
51
- }
52
- r ++ ;
46
+ right ++ ;
53
47
}
54
- console . log ( "mcount" , m , h [ i ] )
55
- if ( m * h [ i ] > max ) {
56
- max = m * h [ i ] ;
48
+ if ( buildings * h [ ith ] > max ) {
49
+ max = buildings * h [ ith ] ;
57
50
}
58
- m = 1 ;
51
+ buildings = 1 ;
59
52
}
60
53
return max ;
61
-
62
54
}
63
55
64
56
function main ( ) {
You can’t perform that action at this time.
0 commit comments