GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — leetcode/python/085_Maximal_Rectangle.py at master · juhan-tarn/leetcode

Expand file tree

/

085_Maximal_Rectangle.py

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
35 lines (32 loc) · 1.2 KB
/

085_Maximal_Rectangle.py

Copy path

File metadata and controls

35 lines (32 loc) · 1.2 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
class Solution(object):
def maximalRectangle(self, matrix):
"""
:type matrix: List[List[str]]
:rtype: int
"""
# https://discuss.leetcode.com/topic/6650/share-my-dp-solution/2
if matrix is None or len(matrix) == 0:
return 0
ls_row, ls_col = len(matrix), len(matrix[0])
left, right, height = [0] * ls_col, [ls_col] * ls_col, [0] * ls_col
maxA = 0
for i in range(ls_row):
curr_left, curr_right = 0, ls_col
for j in range(ls_col):
if matrix[i][j] == '1':
height[j] += 1
else:
height[j] = 0
for j in range(ls_col):
if matrix[i][j] == '1':
left[j] = max(left[j], curr_left)
else:
left[j], curr_left = 0, j + 1
for j in range(ls_col - 1, -1, -1):
if matrix[i][j] == '1':
right[j] = min(right[j], curr_right)
else:
right[j], curr_right = ls_col, j
for j in range(ls_col):
maxA = max(maxA, (right[j] - left[j]) * height[j])
return maxA
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.