Advertisement
kosievdmerwe

Untitled

Nov 29th, 2021
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.14 KB | None | 0 0
  1. class Solution:
  2.     def maximalRectangle(self, matrix: List[List[str]]) -> int:
  3.         W = len(matrix)
  4.         if W == 0:
  5.             return 0
  6.         H = len(matrix[0])
  7.        
  8.        
  9.         heights = [0] * W
  10.         ans = 0
  11.        
  12.         def get_extensions(r: Iterator[int]):
  13.             nonlocal heights
  14.             exts = []
  15.             monostack = []
  16.             for i in r:
  17.                 cur = heights[i]
  18.                 count = 1
  19.                 while monostack and monostack[-1][0] >= cur:
  20.                     _, mcount = monostack.pop()
  21.                     count += mcount
  22.                 monostack.append([cur, count])
  23.                 exts.append(monostack[-1][1])
  24.             return exts
  25.  
  26.         for y in range(H):
  27.             for x in range(W):
  28.                 heights[x] = 0 if matrix[x][y] == "0" else heights[x] + 1
  29.                
  30.             left = get_extensions(range(W))
  31.             right = get_extensions(range(W - 1, -1, -1))
  32.             ans = max(
  33.                 ans,
  34.                 max((left[i] + right[W - 1 - i] - 1) * heights[i] for i in range(W)),
  35.             )
  36.            
  37.         return ans
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement