1.题目描述
Given a 2D binary matrix filled with 0’s and 1’s, find the largest rectangle containing only 1’s and return its area.
给定一个仅包含 0 和 1 的二维二进制矩阵,找出只包含 1 的最大矩形,并返回其面积。
Example:
Input:
[
[“1”,”0”,”1”,”0”,”0”],
[“1”,”0”,”1”,”1”,”1”],
[“1”,”1”,”1”,”1”,”1”],
[“1”,”0”,”0”,”1”,”0”]
]
Output: 6
2.Solutions
This question is similar as [Largest Rectangle in Histogram]:
You can maintain a row length of Integer array H recorded its height of ‘1’s, and scan and update row by row to find out the largest rectangle of each row.
For each row, if matrix[row][i] == ‘1’. H[i] +=1, or reset the H[i] to zero.
and accroding the algorithm of [Largest Rectangle in Histogram], to update the maximum area.
1 | public int maximalRectangle(char[][] matrix) { |
这道题太难了,先搞清楚这些matrix分别代表什么。height
means from top to this position, there are how many ‘1’left
means at current position, what is the index of left bound of the rectangle with height[j]
. 0 means at this position, no rectangle. (现在这个矩形的左边的下标)right
means the right bound index of this rectangle. ‘n’ means no rectangle.
matrix
0 0 0 1 0 0 0
0 0 1 1 1 0 0
0 1 1 1 1 1 0height
0 0 0 1 0 0 0
0 0 1 2 1 0 0
0 1 2 3 2 1 0left
0 0 0 3 0 0 0
0 0 2 3 2 0 0
0 1 2 3 2 1 0right
7 7 7 4 7 7 7
7 7 5 4 5 7 7
7 6 5 4 5 4 7result
0 0 0 1 0 0 0
0 0 3 2 3 0 0
0 5 6 3 6 5 0
dp的太难了,来看下Histogram的解法吧。参考这道题:https://discuss.leetcode.com/topic/7599/o-n-stack-based-java-solution/23
比如这个matrix有n行,就把这个问题转换成n个Histogram的问题。
每个问题就是一个以这一行为底的Histogram问题,上面连续的1的个数就是Height。
1 | public int maximalRectangle(char[][] matrix) { |