题目来源:https://leetcode.com/problems/maximal-rectangle/
问题描述
85. Maximal Rectangle
Hard
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
------------------------------------------------------------
题意
一个矩阵全由’0’和’1’组成,求其中最大的全’1’矩形的面积。
------------------------------------------------------------
思路
【思路1】
作为LeetCode 84. Largest Rectangle in Histogram的扩展,将每行看作一个直方图求最大矩形问题求解,再求所有行的结果的最大值。矩形和行直方图的对应关系由以下例子演示:
1 | 0 | 1 | 0 | 0 |
1 | 0 | 1 | 1 | 1 |
1 | 1 | 1 | 1 | 1 |
1 | 0 | 0 | 1 | 0 |
对应如下行直方图表,第i行是原矩阵的[0:i]行累积成的直方图。
1 | 0 | 1 | 0 | 0 |
2 | 0 | 2 | 1 | 1 |
3 | 1 | 3 | 2 | 2 |
4 | 0 | 0 | 3 | 0 |
由于每行的直方图求最大矩形的复杂度为O(n),故总复杂度为O(n^2),但该方法常数较大。
【思路2】
还是对原矩形每行进行扫描,同时维护每行n个格点的三个数组lefts, rights, heights分别表示该格点所在矩形的最左、最右、最上边界。同时每行维护两个整数ones_left和ones_right,表示当前格点在本行的最边界和右边界(用便于面积计算,右边界实际上加了1)。
最左边界的缺省值是0,最右边界的缺省值是n。每行遍历过程中从左到右更新lefts,同时从右到左更新rights。以lefts[j]和ones_left的更新为例,对于格点matrix[i][j],
- 如果matrix[i][j]=1,则考察上一行的lefts[j],
- 如果上一行的lefts[i]是缺省值0,表示上一行matrix[i-1][j]=0,则本行的lefts[j]表示本行的矩形边界,
- 如果上一行lefts[j]不是0,表示上一行matrix[i-1][j]=1,则本行的lefts[j]表示和上一行共同组成的矩形的边界。
- 如果matrix[i][j]=0,则将lefts[j]置零表示本行j处是0,同时更新ones_left=j+1,表示对于matrix[i][j+1]来说,最左边的1至少在j+1处(当然即使matrix[i][j+1]=0,也不影响,因为此时ones_left又被更新为j+2了)
------------------------------------------------------------
代码
【解法1】
class Solution {
class Pair {
int height, cnt;
public Pair(int height, int cnt)
{
this.height = height;
this.cnt = cnt;
}
}
private int largestRectangleArea(int[] heights) {
Stack<Pair> stack = new Stack<Pair>();
int maxSize = 0, curSize = 0, curCnt = 0;
for (int h: heights)
{
if (stack.empty())
{
stack.push(new Pair(h, 1));
}
else if (h == stack.peek().height)
{
stack.peek().cnt++;
}
else if (h > stack.peek().height)
{
stack.push(new Pair(h, 1));
}
else
{
Pair poped = stack.pop();
curCnt = poped.cnt;
curSize = poped.height*curCnt;
maxSize = curSize>maxSize?curSize:maxSize;
while (!stack.empty() && h < stack.peek().height)
{
poped = stack.pop();
curCnt += poped.cnt;
curSize = poped.height*curCnt;
maxSize = curSize>maxSize?curSize:maxSize;
}
if (stack.empty())
{
stack.push(new Pair(h, curCnt+1));
}
else if (stack.peek().height == h)
{
stack.peek().cnt += curCnt+1;
}
else
{
stack.push(new Pair(h, curCnt+1));
}
}
}
curCnt = 0;
while (!stack.empty())
{
Pair poped = stack.pop();
curCnt += poped.cnt;
curSize = curCnt*poped.height;
maxSize = curSize>maxSize?curSize:maxSize;
}
return maxSize;
}
public int maximalRectangle(char[][] matrix) {
int m = matrix.length;
if (m == 0)
{
return 0;
}
int n = matrix[0].length;
int maxSize = 0, curSize = 0;
int[] heights = new int[n];
for (int i=0; i<m; i++)
{
for (int j=0; j<n; j++)
{
if (matrix[i][j] == '1')
{
heights[j] += 1;
}
else
{
heights[j] = 0;
}
}
curSize = largestRectangleArea(heights);
maxSize = curSize>maxSize?curSize:maxSize;
}
return maxSize;
}
}
【解法2】
class Solution {
public int maximalRectangle(char[][] matrix) {
int m = matrix.length;
if (m == 0)
{
return 0;
}
int n = matrix[0].length, i = 0, j = 0, ones_left = 0, ones_right = n;
int maxSize = 0, curSize = 0;
int[] lefts = new int[n]; // left bound of matrix[i][j]
int[] rights = new int[n]; // right bound of matrix[i][j]
Arrays.fill(rights, n);
int[] heights = new int[n]; // height of matrix[i][j]
for (i=0; i<m; i++) // iterate over rows
{
ones_left = 0;
ones_right = n;
// update heights
for (j=0; j<n; j++)
{
if (matrix[i][j] == '1')
{
heights[j]++;
}
else
{
heights[j] = 0;
}
}
// update lefts
for (j=0; j<n; j++)
{
if (matrix[i][j] == '1')
{
lefts[j] = ones_left > lefts[j]? ones_left: lefts[j];
}
else
{
lefts[j] = 0;
ones_left = j+1;
}
}
// update rights
for (j=n-1; j>=0; j--)
{
if (matrix[i][j] == '1')
{
rights[j] = ones_right < rights[j]? ones_right: rights[j];
}
else
{
rights[j] = n;
ones_right = j;
}
}
// update max size
for (j=0; j<n; j++)
{
maxSize = Math.max(maxSize, heights[j]*(rights[j]-lefts[j]));
}
}
return maxSize;
}
}