-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathsolution.py
More file actions
33 lines (24 loc) · 896 Bytes
/
Copy pathsolution.py
File metadata and controls
33 lines (24 loc) · 896 Bytes
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
from typing import List
class Solution:
def countSquares(self, matrix: List[List[int]]) -> int:
if not matrix or not matrix[0]:
return 0
m, n = len(matrix), len(matrix[0])
dp = [[0] * n for _ in range(m)]
count = 0
# Fill first row
for j in range(n):
dp[0][j] = matrix[0][j]
count += dp[0][j]
# Fill first column
for i in range(1, m):
dp[i][0] = matrix[i][0]
count += dp[i][0]
# Fill rest of the dp table
for i in range(1, m):
for j in range(1, n):
if matrix[i][j] == 1:
# dp[i][j] = min of left, top, and top-left + 1
dp[i][j] = min(dp[i-1][j], dp[i][j-1], dp[i-1][j-1]) + 1
count += dp[i][j]
return count