-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0221__maximal_square.py
84 lines (65 loc) · 2.83 KB
/
0221__maximal_square.py
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
"""
LeetCode: https://leetcode.com/problems/maximal-square/description/
"""
from typing import List, Dict, Tuple
from unittest import TestCase
class Solution(TestCase):
def test_example_1(self):
matrix = [
["1", "0", "1", "0", "0"],
["1", "0", "1", "1", "1"],
["1", "1", "1", "1", "1"],
["1", "0", "0", "1", "0"],
]
self.assertEqual(4, self.maximalSquareTopDown(matrix))
self.assertEqual(4, self.maximalSquareBottomUp(matrix))
def test_example_2(self):
self.assertEqual(1, self.maximalSquareTopDown([["0", "1"], ["1", "0"]]))
self.assertEqual(1, self.maximalSquareBottomUp([["0", "1"], ["1", "0"]]))
def test_example_3(self):
self.assertEqual(0, self.maximalSquareTopDown([["0"]]))
self.assertEqual(0, self.maximalSquareBottomUp([["0"]]))
def test_case_65(self):
matrix = [
["1", "1", "1", "1", "0"],
["1", "1", "1", "1", "0"],
["1", "1", "1", "1", "1"],
["1", "1", "1", "1", "1"],
["0", "0", "1", "1", "1"],
]
self.assertEqual(16, self.maximalSquareTopDown(matrix))
self.assertEqual(16, self.maximalSquareBottomUp(matrix))
def maximalSquareBottomUp(self, matrix: List[List[str]]) -> int:
cache = [[0] * (len(matrix[0]) + 1) for _ in range(len(matrix) + 1)]
max_sq = 0
for row in range(len(matrix)):
for col in range(len(matrix[row])):
if matrix[row][col] == "1":
cache[row][col] = 1 + min(cache[row][col - 1], cache[row - 1][col], cache[row - 1][col - 1])
max_sq = max(max_sq, cache[row][col])
return max_sq ** 2
def maximalSquareTopDown(self, matrix: List[List[str]]) -> int:
max_sq = 0
cache: Dict[Tuple[int, int], int] = {}
def explore(start_row: int, start_column: int) -> int:
if min(start_row, start_column) < 0 or start_row >= len(matrix) or start_column >= len(matrix[start_row]):
return 0
if matrix[start_row][start_column] == "0":
return 0
key = (start_row, start_column)
if key in cache:
return cache[key]
sq = 1
if start_row + 1 < len(matrix) and start_column + 1 < len(matrix[start_row + 1]):
sq += min(
explore(start_row + 1, start_column + 1),
explore(start_row + 1, start_column),
explore(start_row, start_column + 1),
)
cache[key] = sq
return cache[key]
for row in range(len(matrix)):
for col in range(len(matrix[row])):
if matrix[row][col] == "1":
max_sq = max(max_sq, explore(row, col))
return max_sq ** 2