Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area.
方法一:O(n*m^2),计算每一列中的1字组成的竖线能向左扩展多长。
class Solution {
public:
int maximalRectangle(vector<vector<char> > &matrix) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int m = matrix.size();
if(m == 0) return 0;
int n = matrix[0].size();
if(n == 0) return 0;
vector<int> vec(m,0);
vector<vector<int>> area(m,vec);
int maxArea = 0;
for(int i = 0;i<m;i++)
{
for(int j = i;j< m;j++)
{
if(matrix[j][0] == '0') break;
area[i][j] = j-i+1;
if(area[i][j] > maxArea) maxArea = area[i][j];
}
}
for(int k = 1;k<n;k++)
{
for(int i = 0;i<m;i++)
{
int flag = 0;
for(int j = i;j< m;j++)
{
if(flag == 1){
area[i][j] = 0;
}else if(matrix[j][k] == '0') {
flag = 1;
area[i][j] = 0;
}else{
area[i][j] += j-i+1;
if(area[i][j] > maxArea) maxArea = area[i][j];
}
}
}
}
return maxArea;
}
};
244 milli secs.
方法二:一种更好的实现,O(n^2)复杂度。
class Solution {
public:
int maximalRectangle(vector<vector<char> > &matrix) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int m = matrix.size();
if(m == 0) return 0;
int n = matrix[0].size();
if(n == 0) return 0;
vector<int> H(n,0);
vector<int> L(n,-1);
vector<int> R(n,n);
int maxArea = 0;
for(int i = 0;i < m;i++)
{
int left = -1;
int right = n;
for(int j = 0;j < n;j++)
{
if(matrix[i][j] == '1'){
H[j] += 1;
L[j] = max(L[j],left);
}else{
H[j] = 0;
L[j] = -1;
left = j;
}
}
for(int j = n-1;j >= 0;j--)
{
if(matrix[i][j] == '0'){
right = j;
R[j] = n;
}else{
R[j] = min(right,R[j]);
int area = H[j] * (R[j] - L[j]-1);
maxArea = max(maxArea,area);
}
}
}
return maxArea;
}
};
44 milli secs.
A boundary of the maximal rectangle must contain one or more 0's or it is the boundary of the matrix.
Fig. 1: The green rectangle cannot grow any larger since the boundary either contains 0’s or is the boundary of the matrix.
In this problem, we define a "hanging line" to be a subcolumn that contains all 1's except its upper boundary. The topmost can be either a zero or the matrix boundary. Some examples are circled in the same matrix below. A general example is given in Fig.3.
Fig. 2: Three hanging lines are circled above.
Fig. 3: The solid circles are 0's while the lines contain 1's only.
Every point is bijective to a hanging line (From this point to the top nearest 0 or to the matrix boundary). To get a maximal rectangle from a hanging line, we move the line to the left and right as far as possible. To achieve this goal, we use dynamic programming to calculate the leftmost and the rightmost positions that the line can reach to. We also need to know the length of the hanging line to calculate the area. Specifically,
Case 1 -- matrix(i, j) = 1
H(i, j) = H(i-1, j) + 1
L(i, j) = max( L(i-1, j), the position of the left nearest 0 in this row )
R(i, j) = min( R(i-1, j), the position of the right nearest 0 in this row )
Case 2 -- matrix(i, j) = 0
H(i, j) = 0
L(i, j) = 0
R(i, j) = n
详见
http://discuss.leetcode.com/questions/260/maximal-rectangle