-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path25.py
More file actions
30 lines (22 loc) · 737 Bytes
/
25.py
File metadata and controls
30 lines (22 loc) · 737 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
class Solution:
def countSquares(self, matrix: List[List[int]]) -> int:
n = len(matrix)
m = len(matrix[0])
def helper(i, j):
if i < 0 or j < 0:
return 0
if matrix[i][j] == 0:
return 0
if dp[i][j] != -1:
return dp[i][j]
above = helper(i-1, j)
left = helper(i, j-1)
left_diagonal = helper(i-1, j-1)
dp[i][j] = 1 + min(above, left, left_diagonal)
return dp[i][j]
dp = [[-1]*m for i in range(n)]
total = 0
for i in range(n):
for j in range(m):
total += helper(i, j)
return total