221. Maximal Square
窮舉法
rows = len(matrix)
columns = len(matrix[0])
m = min(rows, columns)class Solution:
def maximalSquare(self, matrix: List[List[str]]) -> int:
rows = len(matrix)
columns = len(matrix[0])
L = min(rows, columns) # Longer Side
def is_square(row, col, length):
i = row
while i < (row + length):
j = col
while j < (col + length):
if matrix[i][j] == '0':
return False
j += 1
i += 1
return True
while L > 0:
i = 0
while i < rows - L + 1:
j = 0
while j < columns - L + 1:
if is_square(i, j, L):
return L ** 2
j += 1
i += 1
L -= 1
return 0動態規劃
Last updated