File tree 1 file changed +8
-7
lines changed
scripts/algorithms/M/Maximal Rectangle
1 file changed +8
-7
lines changed Original file line number Diff line number Diff line change
1
+ // Runtime: 64 ms (Top 29.92%) | Memory: 46.6 MB (Top 92.25%)
1
2
class Solution {
2
3
public int maximalRectangle (char [][] matrix ) {
3
4
ArrayList <Integer > list = new ArrayList <>();
4
5
int n = matrix [0 ].length ;
5
6
for (int i =0 ; i <n ; i ++){
6
7
list .add (Character .getNumericValue (matrix [0 ][i ]));
7
8
}
8
-
9
+
9
10
int maxArea = maximumArea (list );
10
11
int m = matrix .length ;
11
-
12
+
12
13
for (int i =1 ; i <m ; i ++){
13
14
for (int j =0 ; j <n ; j ++){
14
15
if (matrix [i ][j ] == '1' ){
@@ -27,7 +28,7 @@ public int maximumArea(ArrayList<Integer> heights) {
27
28
int [] left = new int [n ];
28
29
int [] right = new int [n ];
29
30
int [] width = new int [n ];
30
-
31
+
31
32
for (int i =0 ; i <n ; i ++){
32
33
while (!stack1 .isEmpty () && heights .get (stack1 .peek ()) >= heights .get (i ))
33
34
stack1 .pop ();
@@ -37,7 +38,7 @@ public int maximumArea(ArrayList<Integer> heights) {
37
38
left [i ] = -1 ;
38
39
stack1 .push (i );
39
40
}
40
-
41
+
41
42
for (int i =n -1 ; i >=0 ; i --){
42
43
while (!stack2 .isEmpty () && heights .get (stack2 .peek ()) >= heights .get (i ))
43
44
stack2 .pop ();
@@ -47,14 +48,14 @@ public int maximumArea(ArrayList<Integer> heights) {
47
48
right [i ] = n ;
48
49
stack2 .push (i );
49
50
}
50
-
51
+
51
52
for (int i =0 ; i <n ; i ++){
52
53
width [i ] = right [i ] - left [i ] - 1 ;
53
54
}
54
55
int mxArea = 0 ;
55
56
for (int i =0 ; i <n ; i ++){
56
57
mxArea = Math .max (mxArea , width [i ] * heights .get (i ));
57
- }
58
+ }
58
59
return mxArea ;
59
60
}
60
- }
61
+ }
You can’t perform that action at this time.
0 commit comments