-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfind_largest_square.py
69 lines (62 loc) · 1.68 KB
/
find_largest_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
# http://leetcode.com/problems/maximal-square/
def solve(matrix):
maxCount = 0
# iterate over the matrix
# for each cell check if it contains a 1, if it does, increase the size of the square and check again
for rowIndex, row in enumerate(matrix):
for valIndex, val in enumerate(row):
maxColDim = len(matrix[0]) - valIndex
maxRowDim = len(matrix) - rowIndex
maxDim = maxColDim if maxColDim < maxRowDim else maxRowDim
for i in range(maxDim):
dim = i + 1
if validateOuterEdges(matrix, rowIndex, valIndex, dim):
count = dim * dim
if (count > maxCount):
maxCount = count
else:
break
return maxCount
def validateArea(matrix, rowOffset, itemOffset, dim):
for r in range(dim):
for c in range(dim):
cix = itemOffset + c
rix = rowOffset + r
if matrix[rix][cix] == 0:
return False
return True
def validateOuterEdges(matrix, rowOffset, itemOffset, dim):
# check column
for ro in range(dim):
if matrix[rowOffset+ro][itemOffset+dim-1] == 0:
return False
# check row
for co in range(dim):
if matrix[rowOffset+dim-1][itemOffset+co] == 0:
return False
return True
if __name__ == '__main__':
count = solve([
[1, 0, 1, 0, 0],
[1, 0, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 0, 0, 1, 0]
])
print(f"expected 4 got {count}")
count = solve([
[1, 0, 1, 0, 0, 1],
[1, 0, 1, 1, 1, 0],
[1, 1, 1, 1, 1, 1],
[1, 0, 1, 1, 1, 0],
[1, 0, 1, 0, 0, 1],
])
print(f"expected 9 got {count}")
count = solve([
[0, 1],
[1, 0],
])
print(f"expected 1 got {count}")
count = solve([
[1, 0],
])
print(f"expected 1 got {count}")