Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9683f0d

Browse files
Add medium problems
1 parent 8a93bbc commit 9683f0d

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

‎Leetcode/medium/maximal-square.py‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
"""
2+
# MAXIMAL SQUARE
3+
4+
Given an m x n binary matrix filled with 0's and 1's, find the largest square containing only 1's and return its area.
5+
6+
7+
8+
Example 1:
9+
10+
11+
Input: matrix = [["1","0","1","0","0"],["1","0","1","1","1"],["1","1","1","1","1"],["1","0","0","1","0"]]
12+
Output: 4
13+
Example 2:
14+
15+
16+
Input: matrix = [["0","1"],["1","0"]]
17+
Output: 1
18+
Example 3:
19+
20+
Input: matrix = [["0"]]
21+
Output: 0
22+
23+
24+
Constraints:
25+
26+
m == matrix.length
27+
n == matrix[i].length
28+
1 <= m, n <= 300
29+
matrix[i][j] is '0' or '1'.
30+
"""
31+
32+
class Solution:
33+
def maximalSquare(self, matrix: List[List[str]]) -> int:
34+
35+
m = len(matrix)
36+
n = len(matrix[0])
37+
table = [[0 for _ in range(n)] for _ in range(m)]
38+
maximum = 0
39+
40+
for i in range(m):
41+
table[i][0] = int(matrix[i][0])
42+
maximum = max(maximum, table[i][0])
43+
44+
for i in range(n):
45+
table[0][i] = int(matrix[0][i])
46+
maximum = max(maximum, table[0][i])
47+
48+
for i in range(1, m):
49+
for j in range(1, n):
50+
if matrix[i][j] == '0':
51+
continue
52+
table[i][j] = min(table[i - 1][j], table[i][j - 1], table[i - 1][j - 1]) + 1
53+
maximum = max(maximum, table[i][j])
54+
55+
for row in table:
56+
print(row)
57+
return maximum ** 2
58+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /