Skip to content

Commit f87d23d

Browse files
authored
Create 221-Maximal-Square.py
1 parent bb01924 commit f87d23d

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

221-Maximal-Square.py

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution:
2+
def maximalSquare(self, matrix: List[List[str]]) -> int:
3+
ROWS, COLS = len(matrix), len(matrix[0])
4+
cache = {} # map each (r, c) -> maxLength of square
5+
6+
def helper(r, c):
7+
if r >= ROWS or c >= COLS:
8+
return 0
9+
10+
if (r, c) not in cache:
11+
down = helper(r + 1, c)
12+
right = helper(r, c + 1)
13+
diag = helper(r + 1, c + 1)
14+
15+
cache[(r, c)] = 0
16+
if matrix[r][c] == "1":
17+
cache[(r, c)] = 1 + min(down, right, diag)
18+
return cache[(r, c)]
19+
helper(0, 0)
20+
return max(cache.values()) ** 2

0 commit comments

Comments
 (0)