Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.06 KB | None | 0 0
  1. class Solution(object):
  2. def maximalRectangle(self, matrix):
  3. if not matrix or len(matrix) == 0: return 0
  4. res, rows, columns = 0, len(matrix), len(matrix[0])
  5. for r in xrange(rows):
  6. for c in xrange(columns):
  7. if r == 0: matrix[r][c] = int(matrix[r][c])
  8. else:
  9. if matrix[r][c] == '1': matrix[r][c] = 1 + matrix[r - 1][c]
  10. else: matrix[r][c] = 0
  11. res = max(res, self.largestRectangleArea(matrix[r]))
  12. return res
  13.  
  14. def largestRectangleArea(self, heights):
  15. stack, res, i, n = [], 0, 0, len(heights)
  16. while i <= n:
  17. if i == n: h = 0
  18. else: h = heights[i]
  19.  
  20. if len(stack) == 0 or h >= heights[stack[-1]]:
  21. stack.append(i)
  22. i += 1
  23. else:
  24. curr = heights[stack.pop()]
  25. if len(stack) == 0: res = max(res, curr * i)
  26. else: res = max(res, curr * (i - 1 - stack[-1]))
  27. return res
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement