-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaxRectangle.java
More file actions
57 lines (56 loc) · 1.79 KB
/
MaxRectangle.java
File metadata and controls
57 lines (56 loc) · 1.79 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
50
51
52
53
54
55
56
57
public class MaxRectangle {
public int maximalRectangle(char[][] matrix) {
if(matrix.length == 0) return 0;
int rows = matrix.length;
int cols = matrix[0].length;
int max = 0;
int[] hist = new int[cols +1];
for(int i = 0; i < rows; i++){
for(int j = 0; j < cols; j++){
if(matrix[i][j] == '1'){
hist[j]+= 1;
}else{
hist[j] = 0;
}
}
hist[hist.length -1] = 0;
max = Math.max(max, maxRecHist(hist));
}
return max;
}
public int maxRecHist(int[] hist){
Stack<Integer> ss = new Stack<Integer>();
int max = 0;
for(int i = 0; i < hist.length; i++){
while(!ss.isEmpty() && hist[i] < hist[ss.peek()]){
max = Math.max(max, hist[ss.pop()]* (ss.isEmpty()? i: i - ss.peek() -1));
}
ss.push(i);
}
return max;
}
/*
public int maximalRectangle(char[][] matrix) {
int rows = matrix.length;
if(rows == 0) return 0;
int cols = matrix[0].length;
int max =0;
// topleft coord:(r, c)
for(int r = 0; r < rows; r++){
for(int c = 0; c < cols; c++){
int[] sumC = new int[cols - c];
int[] sumR = new int[cols - c];
//bottomright coord:(x, y)
for(int x = r; x < rows; x++){
for(int y = c; y < cols; y++){
sumC[y - c] = (y -c -1 >= 0 ? sumC[y -c - 1]: 0) + matrix[x][y];
sumR[y - c] += sumR[y -c];
max = Math.max(max, sumR[y - c]);
}
}
}
}
return max;
}
*/
}