forked from ghostmkg/programming-language
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSquare_SubMat_With_Ones.py
More file actions
47 lines (39 loc) · 1.69 KB
/
Square_SubMat_With_Ones.py
File metadata and controls
47 lines (39 loc) · 1.69 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
from typing import List
class Solution:
def countSquares(self, matrix: List[List[int]]) -> int:
# Get number of rows and columns
r = len(matrix)
c = len(matrix[0])
# dp[i][j] will store the size of the largest square
# whose bottom-right corner is at cell (i, j)
dp = [[0] * c for _ in range(r)]
count = 0 # To store total number of square submatrices
# Initialize first row
# Each 1 in the first row forms a 1x1 square
for j in range(c):
if matrix[0][j] == 1:
dp[0][j] = 1
count += 1
# Initialize first column
# Each 1 in the first column forms a 1x1 square
for i in range(1, r):
if matrix[i][0] == 1:
dp[i][0] = 1
count += 1
# Fill the dp table for the rest of the matrix
for i in range(1, r):
for j in range(1, c):
# If the cell is 0, no square can end here
if matrix[i][j] == 0:
continue
# If the cell is 1, find the smallest neighboring square
# and extend it by 1 to form a bigger square
elif dp[i-1][j] == 0 or dp[i-1][j-1] == 0 or dp[i][j-1] == 0:
dp[i][j] = 1 # Only a 1x1 square possible
else:
# Minimum of top, left, and top-left + 1
dp[i][j] = min(dp[i-1][j], dp[i-1][j-1], dp[i][j-1]) + 1
# Add the number of squares ending at (i, j)
count += dp[i][j]
# Return total count of all square submatrices with all 1s
return count