(Week 10 算法博客)
题目
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
Difficulty: Hard
分析
这道题是在“动态规划”主题里找到的,可以用动态规划来做。
算法1
对初始矩阵里的每个点 (i,j)
,都有一个在这个点右下方的搜索范围 (i,j) ~ (row-1, col-1)
,姑且称它为搜索矩阵。需要检查每个点,在它的搜索矩阵里进行对全1矩阵的搜索。
事实上,一个全1矩阵的大小可以由左上角顶点和右下角顶点的坐标确定。那么怎么确定某两个顶点之间的矩阵是全1矩阵呢?
这样做:保证搜索到的、每个为1的顶点都在一个全1矩阵里。即是说:搜索到0,就停止搜索。
如果某一处有0,那么在下面的行之中、处于这一列的点,是不可能再与 (i,j)
构成全1矩阵的。由此,可以确定下一行的搜索范围。
newcol
是一开始的列搜索范围。在搜索矩阵里逐行做逐列的检查,如果 (i+m,j+n)
不为1,就更新 newcol
为 n
,停止这一行的逐列检查,开始下一行的检查。
如果 (i+m,j+n)
不为1,而 n
等于0,即这个点与 (i,j)
在同一列,那么就不再检查接下来的行了。接下来的行不可能再与 (i,j)
构成全1矩阵。
如图:
对搜索到的每个为1的顶点,都计算它与 (i,j)
之间的矩阵大小,并由此计算最大的全1矩阵的大小。
C++代码如下:
class Solution {
public:
int maximalRectangle(vector<vector<char>>& matrix) {
int row = matrix.size();
if(!row) return 0;
int col = matrix[0].size();
const int size = row * col;
int maxArea = 0;
int point = 0;
for(int i = 0; i < row; i++){
for(int j = 0; j < col; j++){
int newrow = row - i;
int newcol = col - j;
int newsize = newrow * newcol;
for(int m = 0; m < newrow; m++){
for(int n = 0; n < newcol; n++){
if(matrix[i + m][j + n] != '1'){
if(n == 0){
newrow = m;
break;
}
newcol = n;
continue;
}
int newArea = (m + 1) * (n + 1);
if(maxArea < newArea) maxArea = newArea;
}
}
point++;
}
}
return maxArea;
}
};
用时为 。
为输入矩阵的大小,该算法的时间复杂度为 ,空间复杂度为 。
算法2
这个算法来自题目下面的讨论:Share my DP solution
思路如下:
- 把matrix看成多个直方图,每一行及其上方的数据都构成一个直方图,需要考察matrix.size()个直方图。
- 对于每个点(row, col),最后都计算以这个点上方的连续的’1’往left, right两个方向延伸可以得到的最大的矩形的面积。
- 通过这种方法获取的矩形一定会把最大的矩形包含在内。
- height[row][col]记录的是(row, col)这个坐标为底座的直方图柱子的高度,如果这个点是’0’,那么高度当然是0了。
- left[row][col]记录的是(row, col)这个坐标点对应的height可以延伸到的最左边的位置。
- right[row][col]记录的是(row, col)这个坐标点对应的height可以延伸到的最右边的位置+1。
- height[row][col]、left[row][col]、right[row][col]都受到height[row][col]/left[row-1][col]/right[row-1][col]的制约,所以可以把height/left/right作为一维的数组,逐行更新。
例子:
0 0 0 1 0 0 0
0 0 1 1 1 0 0
0 1 1 1 1 1 0
row | height | left | right |
---|---|---|---|
0 | 0 0 0 1 0 0 0 | 0 0 0 3 0 0 0 | 7 7 7 4 7 7 7 |
1 | 0 0 1 2 1 0 0 | 0 0 2 3 2 0 0 | 7 7 5 4 5 7 7 |
2 | 0 1 2 3 2 1 0 | 0 1 2 3 2 1 0 | 7 6 5 4 5 6 7 |
C++代码如下:
class Solution {
public:
int maximalRectangle(vector<vector<char> > &matrix) {
if(matrix.empty()) return 0;
const int m = matrix.size();
const int n = matrix[0].size();
int left[n], right[n], height[n];
fill_n(left,n,0); fill_n(right,n,n); fill_n(height,n,0);
int maxA = 0;
for(int i=0; i<m; i++) {
int cur_left=0, cur_right=n;
for(int j=0; j<n; j++) { // compute height (can do this from either side)
if(matrix[i][j]=='1') height[j]++;
else height[j]=0;
}
for(int j=0; j<n; j++) { // compute left (from left to right)
if(matrix[i][j]=='1') left[j]=max(left[j],cur_left);
else {left[j]=0; cur_left=j+1;}
}
// compute right (from right to left)
for(int j=n-1; j>=0; j--) {
if(matrix[i][j]=='1') right[j]=min(right[j],cur_right);
else {right[j]=n; cur_right=j;}
}
// compute the area of rectangle (can do this from either side)
for(int j=0; j<n; j++)
maxA = max(maxA,(right[j]-left[j])*height[j]);
}
return maxA;
}
};
用时为 ,时间复杂度为 ,空间复杂度为 。
小结
我的算法1有一处不足:会重复搜索某些地方,不算是好的动态规划。(甚至不知道它算不算是动态规划。)
而算法2没有重复的问题,较好地利用了之前的状态。
这道题的难度就在于如何不重复地利用之前的状态进行动态规划,尤其是,这道题存在重叠的全1矩阵。