forked from algorithm009-class01/algorithm009-class01
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaximalRectangle.java
More file actions
49 lines (45 loc) · 1.56 KB
/
MaximalRectangle.java
File metadata and controls
49 lines (45 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package str;
import java.util.Arrays;
public class MaximalRectangle {
public int maximalRectangle(char[][] matrix){
if (matrix == null || matrix.length == 0 || matrix[0].length == 0 || matrix[0]==null) return 0;
int m = matrix.length,n = matrix[0].length,maxArea = 0;
int[] left = new int[n];
int[] right = new int[n];
int[] height = new int[n];
Arrays.fill(right,n-1);
for (int i = 0; i < m; i++) {
int rB = n -1;
for (int j = n-1; j >= 0 ; j--) {
if (matrix[i][j] == '1'){
right[j] = Math.min(right[j],rB);
}else {
right[j] = n -1;
rB = j - 1;
}
}
int lB = 0;
for (int j =0;j<n;j++){
if (matrix[i][j] == '1'){
left[j] = Math.max(left[j],lB);
height[j]++;
maxArea = Math.max(maxArea,height[j]*(right[j]-left[j]+1));
}else {
height[j] = 0;
left[j] = 0;
lB = j+1;
}
}
}
return maxArea;
}
public static void main(String[] args) {
char[][] matrix = {{'1','0','1','0','0'},
{'1','0','1','1','1'},
{'1','1','1','1','1'},
{'1','0','0','1','0'}};
MaximalRectangle maximalRectangle = new MaximalRectangle();
int r = maximalRectangle.maximalRectangle(matrix);
System.out.println(r);
}
}