Leetcode 85. Maximal Rectangle
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
class Solution:
def maximalRectangle(self, matrix: List]) -> int:
if matrix == None or len(matrix) == 0 or len(matrix) == 0:
return 0
m = len(matrix)
n = len(matrix)
height = * n
res = 0
for row in matrix:
for i in range(n):
height = height + 1 if row == '1' else 0
stack = []
for i in range(n + 1):
if i == n:
curt = -1
else:
curt = height
while stack and curt <= height]:
h = height
if len(stack) == 0:
w = i
else:
w = i - stack[-1] - 1
res = max(res, h * w)
stack.append(i)
return res
页:
[1]