题目地址: https://leetcode.com/problems/maximal-rectangle/description/
题目描述:
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
题目大意
给出了一个二维的数组,求在这里面能够成的最大的矩形面积是多少。
解题方法
本以为会和221. Maximal Squareopen in new window做法一样使用DP解决,可是感觉状态转移方程太复杂,所以还是参考了84. Largest Rectangle in Histogramopen in new window升级版的解法。
如图所示,如果把每一行的1和它上面的1连在一起,那么就可以看成一个个站里的矩形方块。那么我们的最终目的就是找出最大面积的矩形方块,所以就是第84题的做法了,使用单调栈。
需要注意的是,我们使用一个height数组,保存到某一层的第i个位置为止,能向上构成的矩形的高度。而且需要对每层都做一个寻找面积的操作,最终选择所有层中能够成矩形面积最大值。
时间复杂度是O(M(N+M)),空间复杂度是O(N)。
class Solution(object):
def maximalRectangle(self, matrix):
"""
:type matrix: List[List[str]]
:rtype: int
"""
if not matrix or not matrix[0]: return 0
M, N = len(matrix), len(matrix[0])
height = [0] * N
res = 0
for row in matrix:
for i in range(N):
if row[i] == '0':
height[i] = 0
else:
height[i] += 1
res = max(res, self.maxRectangleArea(height))
return res
def maxRectangleArea(self, height):
if not height: return 0
res = 0
stack = list()
height.append(0)
for i in range(len(height)):
cur = height[i]
while stack and cur < height[stack[-1]]:
w = height[stack.pop()]
h = i if not stack else i - stack[-1] - 1
res = max(res, w * h)
stack.append(i)
return res
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
DDKK.COM 弟弟快看-教程,程序员编程资料站,版权归原作者所有
本文经作者:负雪明烛 授权发布,任何组织或个人未经作者授权不得转发