class Solution:
# @param matrix, a list of lists of 1 length string
# @return an integer
def largestRectangleArea(self, height):
height.insert(0,0)
height.append(0)
n=len(height)
stack=[0]
maxRec=0
for i in range(1,n):
while height[i]<height[stack[-1]]:
k=stack.pop()
newarea=(i-1-stack[-1])*height[k]
if newarea>maxRec:
maxRec=newarea
stack.append(i)
height.pop()
height.pop(0)
return maxRec
def maximalRectangle(self, matrix):
maxRec=0
m=len(matrix)
if m>0:
n=len(matrix[0])
else:
return 0
height=[[0 for i in range(n)] for j in range(m)]
for i in range(m):
for j in range(n):
if i==0:
height[i][j]=int(matrix[i][j])
elif matrix[i][j]=='0':
height[i][j]=0
else :
height[i][j]=height[i-1][j]+1
maxRec=max(maxRec,self.largestRectangleArea(height[i]))
return maxRec
Thursday, September 18, 2014
Leetcode: Maximal Rectangle @Python
Subscribe to:
Post Comments
(
Atom
)
No comments :
Post a Comment