Skip to content

Commit ea3b159

Browse files
committed
Runtime: 64 ms (Top 29.92%) | Memory: 46.6 MB (Top 92.25%)
1 parent efe3366 commit ea3b159

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

scripts/algorithms/M/Maximal Rectangle/Maximal Rectangle.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1+
// Runtime: 64 ms (Top 29.92%) | Memory: 46.6 MB (Top 92.25%)
12
class Solution {
23
public int maximalRectangle(char[][] matrix) {
34
ArrayList<Integer> list = new ArrayList<>();
45
int n = matrix[0].length;
56
for(int i=0; i<n; i++){
67
list.add(Character.getNumericValue(matrix[0][i]));
78
}
8-
9+
910
int maxArea = maximumArea(list);
1011
int m = matrix.length;
11-
12+
1213
for(int i=1; i<m; i++){
1314
for(int j=0; j<n; j++){
1415
if(matrix[i][j] == '1'){
@@ -27,7 +28,7 @@ public int maximumArea(ArrayList<Integer> heights) {
2728
int[] left = new int[n];
2829
int[] right = new int[n];
2930
int[] width = new int[n];
30-
31+
3132
for(int i=0; i<n; i++){
3233
while(!stack1.isEmpty() && heights.get(stack1.peek()) >= heights.get(i))
3334
stack1.pop();
@@ -37,7 +38,7 @@ public int maximumArea(ArrayList<Integer> heights) {
3738
left[i] = -1;
3839
stack1.push(i);
3940
}
40-
41+
4142
for(int i=n-1; i>=0; i--){
4243
while(!stack2.isEmpty() && heights.get(stack2.peek()) >= heights.get(i))
4344
stack2.pop();
@@ -47,14 +48,14 @@ public int maximumArea(ArrayList<Integer> heights) {
4748
right[i] = n;
4849
stack2.push(i);
4950
}
50-
51+
5152
for(int i=0; i<n; i++){
5253
width[i] = right[i] - left[i] - 1;
5354
}
5455
int mxArea = 0;
5556
for(int i=0; i<n; i++){
5657
mxArea = Math.max(mxArea, width[i] * heights.get(i));
57-
}
58+
}
5859
return mxArea;
5960
}
60-
}
61+
}

0 commit comments

Comments
 (0)