-
Notifications
You must be signed in to change notification settings - Fork 17
/
count-submatrices-with-all-ones.py
50 lines (36 loc) · 1.47 KB
/
count-submatrices-with-all-ones.py
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
48
49
50
from typing import List
def matrices_in_rect(rows: int, cols: int) -> int:
return int((cols * (cols + 1) / 2) * (rows * (rows + 1) / 2))
class Solution:
def numSubmat(self, mat: List[List[int]]) -> int:
def count_rectangles(start_row: int, start_col: int) -> int:
rows, cols = len(mat), len(mat[0]) if mat else 0
count = 0
max_col = cols
for row in range(start_row, rows):
if mat[row][start_col] != 1:
break
for col in range(start_col, max_col):
if mat[row][col] != 1:
break
max_col = col + 1
count += 1
return count
rows, cols = len(mat), len(mat[0]) if mat else 0
count = 0
for row in range(rows):
for col in range(cols):
count += count_rectangles(row, col)
return count
def numSubmat1(self, mat: List[List[int]]) -> int:
rows, cols = len(mat), len(mat[0]) if mat else 0
sizes = [[(0, 0)] * (cols + 1) for _ in range(rows + 1)]
for row in range(rows):
for col in range(cols):
if mat[row][col] == 1:
sizes[row + 1][col + 1] = (
min(sizes[row][col + 1][0], sizes[row][col][0]) + 1,
min(sizes[row + 1][col][1], sizes[row][col][1]) + 1,
)
print(sizes)
return 0