forked from shuboc/LeetCode-2
-
Notifications
You must be signed in to change notification settings - Fork 6
/
maximal-square.py
127 lines (110 loc) · 4.08 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
# Time: O(n^2)
# Space: O(n)
#
# Given a 2D binary matrix filled with 0's and 1's,
# find the largest square containing all 1's and return its area.
#
# For example, given the following matrix:
#
# 1 0 1 0 0
# 1 0 1 1 1
# 1 1 1 1 1
# 1 0 0 1 0
# Return 4.
#
# DP with sliding window.
class Solution:
# @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 xrange(n)] for i in xrange(2)]
max_size = 0
for j in xrange(n):
if matrix[0][j] == '1':
size[0][j] = 1
max_size = max(max_size, size[0][j])
for i in xrange(1, m):
if matrix[i][0] == '1':
size[i % 2][0] = 1
else:
size[i % 2][0] = 0
for j in xrange(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:
# @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 xrange(n)] for i in xrange(m)]
max_size = 0
for j in xrange(n):
if matrix[0][j] == '1':
size[0][j] = 1
max_size = max(max_size, size[0][j])
for i in xrange(1, m):
if matrix[i][0] == '1':
size[i][0] = 1
else:
size[i][0] = 0
for j in xrange(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
# Time: O(n^2)
# Space: O(n^2)
# DP.
class Solution3:
# @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 xrange(len(matrix[0]))] \
for i in xrange(len(matrix))]
for i in reversed(xrange(len(matrix))):
for j in reversed(xrange(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 xrange(len(matrix[0]))] \
for i in xrange(len(matrix))]
max_square_area = 0
for i in reversed(xrange(len(matrix))):
for j in reversed(xrange(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;