Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing only 1's and return its area.
Example:
Input:
[
["1","0","1","0","0"],
["1","0","1","1","1"],
["1","1","1","1","1"],
["1","0","0","1","0"]
]
Output: 6
方法1:
class Solution {
public int maximalRectangle(char[][] matrix) {
if(matrix==null || matrix.length==0)
return 0;
int res = 0;
int m = matrix.length;
int n = matrix[0].length;
int[] left = new int[n];
int[] right = new int[n];
int[] height = new int[n];
Arrays.fill(right,n);
for(int i = 0; i<m; i++){
int curleft = 0, curright = n;
for(int j = 0; j<n;j++){
if(matrix[i][j]=='1')
height[j]++;
else
height[j] = 0;
}
for(int j = 0;j<n; j++){
if(matrix[i][j]=='1'){
left[j]=Math.max(left[j],curleft);
}else{
left[j]=0;
curleft = j+1;
}
}
for(int j = n-1;j>=0;j--){
if(matrix[i][j]=='1')
right[j] = Math.min(right[j],curright);
else{
right[j] = n;
curright = j;
}
}
for(int j = 0; j<n;j++){
res = Math.max(res,(right[j]-left[j])*height[j]);
}
}
return res;
}
}
时间复杂度:O(n^3)
空间复杂度:O(n)
源码github地址:https://github.com/zhangyu345293721/leetcode