-
Notifications
You must be signed in to change notification settings - Fork 43
/
maximal-square.py
188 lines (157 loc) · 5.57 KB
/
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
"""
221. Maximal Square
Medium
Given an m x n binary matrix filled with 0's and 1's, find the largest square containing only 1's and return its area.
Example 1:
Input: matrix = [["1","0","1","0","0"],["1","0","1","1","1"],["1","1","1","1","1"],["1","0","0","1","0"]]
Output: 4
Example 2:
Input: matrix = [["0","1"],["1","0"]]
Output: 1
Example 3:
Input: matrix = [["0"]]
Output: 0
Constraints:
m == matrix.length
n == matrix[i].length
1 <= m, n <= 300
matrix[i][j] is '0' or '1'.
"""
# V0
# V1
# http://bookshadow.com/weblog/2015/06/03/leetcode-maximal-square/
# dynamic programming state equation :
# dp[x][y] = min(dp[x - 1][y - 1], dp[x][y - 1], dp[x - 1][y]) + 1
class Solution:
# @param {character[][]} matrix
# @return {integer}
def maximalSquare(self, matrix):
if matrix == []:
return 0
m, n = len(matrix), len(matrix[0])
dp = [[0] * n for x in range(m)]
ans = 0
for x in range(m):
for y in range(n):
dp[x][y] = int(matrix[x][y])
if x and y and dp[x][y]:
dp[x][y] = min(dp[x - 1][y - 1], dp[x][y - 1], dp[x - 1][y]) + 1
ans = max(ans, dp[x][y])
return ans * ans
# V1'
# https://blog.csdn.net/fuxuemingzhu/article/details/82992233
class Solution(object):
def maximalSquare(self, matrix):
"""
:type matrix: List[List[str]]
:rtype: int
"""
if not matrix: return 0
M = len(matrix)
N = len(matrix[0])
dp = [[0] * N for _ in range(M)]
for i in range(M):
dp[i][0] = int(matrix[i][0])
for j in range(N):
dp[0][j] = int(matrix[0][j])
for i in range(1, M):
for j in range(1, N):
if int(matrix[i][j]) == 1:
dp[i][j] = min(dp[i][j - 1], dp[i - 1][j], dp[i - 1][j - 1]) + 1
return max(map(max, dp)) ** 2
# V2
# Time: O(n^2)
# Space: O(n)
class Solution(object):
# @param {character[][]} matrix
# @return {integer}
def maximalSquare(self, matrix):
if not matrix:
return 0
m, n = len(matrix), len(matrix[0])
size = [[0 for j in range(n)] for i in range(2)]
max_size = 0
for j in range(n):
if matrix[0][j] == '1':
size[0][j] = 1
max_size = max(max_size, size[0][j])
for i in range(1, m):
if matrix[i][0] == '1':
size[i % 2][0] = 1
else:
size[i % 2][0] = 0
for j in range(1, n):
if matrix[i][j] == '1':
size[i % 2][j] = min(size[i % 2][j - 1], \
size[(i - 1) % 2][j], \
size[(i - 1) % 2][j - 1]) + 1
max_size = max(max_size, size[i % 2][j])
else:
size[i % 2][j] = 0
return max_size * max_size
# Time: O(n^2)
# Space: O(n^2)
# DP.
class Solution2(object):
# @param {character[][]} matrix
# @return {integer}
def maximalSquare(self, matrix):
if not matrix:
return 0
m, n = len(matrix), len(matrix[0])
size = [[0 for j in range(n)] for i in range(m)]
max_size = 0
for j in range(n):
if matrix[0][j] == '1':
size[0][j] = 1
max_size = max(max_size, size[0][j])
for i in range(1, m):
if matrix[i][0] == '1':
size[i][0] = 1
else:
size[i][0] = 0
for j in range(1, n):
if matrix[i][j] == '1':
size[i][j] = min(size[i][j - 1], \
size[i - 1][j], \
size[i - 1][j - 1]) + 1
max_size = max(max_size, size[i][j])
else:
size[i][j] = 0
return max_size * max_size
# V2
class Solution3(object):
# @param {character[][]} matrix
# @return {integer}
def maximalSquare(self, matrix):
if not matrix:
return 0
H, W = 0, 1
# DP table stores (h, w) for each (i, j).
table = [[[0, 0] for j in range(len(matrix[0]))] \
for i in range(len(matrix))]
for i in reversed(range(len(matrix))):
for j in reversed(range(len(matrix[i]))):
# Find the largest h such that (i, j) to (i + h - 1, j) are feasible.
# Find the largest w such that (i, j) to (i, j + w - 1) are feasible.
if matrix[i][j] == '1':
h, w = 1, 1
if i + 1 < len(matrix):
h = table[i + 1][j][H] + 1
if j + 1 < len(matrix[i]):
w = table[i][j + 1][W] + 1
table[i][j] = [h, w]
# A table stores the length of largest square for each (i, j).
s = [[0 for j in range(len(matrix[0]))] \
for i in range(len(matrix))]
max_square_area = 0
for i in reversed(range(len(matrix))):
for j in reversed(range(len(matrix[i]))):
side = min(table[i][j][H], table[i][j][W])
if matrix[i][j] == '1':
# Get the length of largest square with bottom-left corner (i, j).
if i + 1 < len(matrix) and j + 1 < len(matrix[i + 1]):
side = min(s[i + 1][j + 1] + 1, side)
s[i][j] = side
max_square_area = max(max_square_area, side * side)
return max_square_area