Advertisement
serega1112

85

Dec 24th, 2020
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.51 KB | None | 0 0
  1. class Solution:
  2.     def maximalRectangle(self, matrix: List[List[str]]) -> int:
  3.        
  4.         def max_hist_area(col):
  5.             n = len(matrix)
  6.             prev_lt = [i for i in range(n)]
  7.             st_prev = []
  8.             next_lt = [n - i - 1 for i in range(n)]
  9.             st_next = []
  10.            
  11.             for row in range(n):
  12.                 while st_prev and matrix[row][col] <= matrix[st_prev[-1]][col]:
  13.                     st_prev.pop()
  14.                 if st_prev:
  15.                     prev_lt[row] = row - st_prev[-1] - 1
  16.                 st_prev.append(row)
  17.  
  18.                 while st_next and matrix[row][col] < matrix[st_next[-1]][col]:
  19.                     x = st_next.pop()
  20.                     next_lt[x] = row - x - 1
  21.                 st_next.append(row)
  22.            
  23.             res = 0
  24.            
  25.             for row in range(n):
  26.                 res = max(res, (1 + prev_lt[row] + next_lt[row]) * matrix[row][col])
  27.                
  28.             return res
  29.          
  30.        
  31.         for i in range(len(matrix)):
  32.             cur_max = 0
  33.             for j in range(len(matrix[i])):
  34.                 if matrix[i][j] == '1':
  35.                     matrix[i][j] = cur_max + 1
  36.                     cur_max += 1
  37.                 else:
  38.                     matrix[i][j] = 0
  39.                     cur_max = 0
  40.                    
  41.         res = 0
  42.         if matrix:
  43.             for col in range(len(matrix[0])):
  44.                 res = max(res, max_hist_area(col))
  45.            
  46.         return res
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement