-
Notifications
You must be signed in to change notification settings - Fork 0
/
85.maximal-rectangle.java
53 lines (47 loc) · 1.62 KB
/
85.maximal-rectangle.java
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
class Solution {
public int maximalRectangle(char[][] matrix) {
if(matrix.length == 0)
return 0;
int[][] intMat = new int[matrix.length][matrix[0].length];
for(int i=0;i<matrix[0].length;i++){
for(int j=0;j<matrix.length;j++){
if(j == 0)
intMat[j][i] = matrix[j][i]-'0';
else if(matrix[j][i] != '0'){
intMat[j][i] = matrix[j][i]-'0' + intMat[j-1][i];
}
}
}
int area = 0;
for(int i=0;i<intMat.length;i++){
int min = 5000;
// System.out.println(Arrays.toString(intMat[i]));
int j = 0;
int maxind = 0;
Stack<Integer> stk = new Stack<>();
while(j<intMat[i].length){
if(stk.isEmpty()){
stk.push(j++);
}else if(intMat[i][stk.peek()] <= intMat[i][j]){
stk.push(j++);
}
else{
int ind = stk.pop();
if(stk.isEmpty())
area = Math.max(area, intMat[i][ind] * j);
else
area = Math.max(area, intMat[i][ind] * (j-stk.peek()-1));
}
// j++;
}
while(!stk.isEmpty()){
int ind = stk.pop();
if(stk.isEmpty())
area = Math.max(area, intMat[i][ind] * j);
else
area = Math.max(area, intMat[i][ind] * (j-stk.peek()-1));
}
}
return area;
}
}