221 Maximal Square
1 0 1 0 0
1 0 1 1 1
1 1 1 1 1
1 0 0 1 0 0 0 0 0 0 0
0 1 0 1 0 0
0 1 0 1 1 1
0 1 1 1 2 2
0 1 0 0 1 00 0 0 0 0
0 1 1 1 1
0 1 2 2 2
0 1 2 3 3
0 1 2 3 4Last updated
1 0 1 0 0
1 0 1 1 1
1 1 1 1 1
1 0 0 1 0 0 0 0 0 0 0
0 1 0 1 0 0
0 1 0 1 1 1
0 1 1 1 2 2
0 1 0 0 1 00 0 0 0 0
0 1 1 1 1
0 1 2 2 2
0 1 2 3 3
0 1 2 3 4Last updated
import numpy as np
class Solution:
def maximalSquare(self, matrix):
"""
:type matrix: List[List[str]]
:rtype: int
"""
if not matrix:
return 0
Matrix = np.zeros((len(matrix) + 1, len(matrix[0]) + 1))
Matrix[1:Matrix.shape[0], 1:Matrix.shape[1]] = np.array(matrix, dtype=int)
the_max = 0
for i in range(1, Matrix.shape[0]):
for j in range(1, Matrix.shape[1]):
if Matrix[i][j] == 1:
Matrix[i][j] = min(Matrix[i - 1, j], Matrix[i - 1, j - 1], Matrix[i, j - 1]) + 1
the_max = max(the_max, int(Matrix[i][j]))
return the_max*the_max
t1 = [['1', '0', '1', '0', '0'],
['1', '0', '1', '1', '1'],
['1', '1', '1', '1', '1'],
['1', '0', '0', '1', '0']]
obj = Solution()
print(obj.maximalSquare(t1))