-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathsolution.py
39 lines (31 loc) · 1016 Bytes
/
solution.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
#!/usr/bin/env python
# created by BBruceyuan on 18-5-2
import numpy as np
class Solution:
def maximalSquare(self, matrix):
"""
:type matrix: List[List[str]]
:rtype: int
"""
if len(matrix) == 0:
return 0
matrix = np.array(matrix)
row, column = len(matrix), len(matrix[0])
dp = np.zeros((row + 1, column + 1))
print(dp)
max_sqlen = 0
for i in range(1, row + 1):
for j in range(1, column + 1):
if matrix[i - 1][j - 1] == '1':
dp[i][j] = min(dp[i][j - 1], dp[i - 1][j], dp[i - 1][j - 1]) + 1
max_sqlen = max(max_sqlen, dp[i][j])
return int(max_sqlen ** 2)
def main():
s = Solution()
a = s.maximalSquare([["1","0","1","0","0"],
["1","0","1","1","1"],
["1","1","1","1","1"],
["1","0","0","1","0"]])
print(a)
if __name__ == '__main__':
main()