85 Maximal Rectangle
时间上不是很优化,TODO
public class Solution {
public static void main(String[] args){
}
public int maximalRectangle(char[][] matrix) {
int m = matrix.length;
if(m==0) return 0;
int n = matrix[0].length;
int max = 0;
int[] h = new int[n+1];
for(int i=0;i<m;i++){
Stack<Integer> s = new Stack<Integer>();
for(int j=0;j<n+1;j++){
if(j<n){
if(matrix[i][j]=='1'){
h[j]+=1;
}else{
h[j]=0;
}
}
if(s.empty()||h[j]>=h[s.peek()]){
s.push(j);
}else{
while(!s.empty()&&h[j]<h[s.peek()]){
int tp = s.pop();
int sum = h[tp]*(s.empty()?j:j-1-s.peek());
max = max>sum?max:sum;
}
s.push(j);
}
}
}
return max;
}
}