-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCountSubmatricesWithAllOnes.py
More file actions
42 lines (33 loc) · 1.13 KB
/
CountSubmatricesWithAllOnes.py
File metadata and controls
42 lines (33 loc) · 1.13 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
#Time-Complexity-O(m*n)
#Space-Complexity-O(n)
from typing import List
class Solution:
def numSubmat(self, mat: List[List[int]]) -> int:
rows = len(mat)
cols = len(mat[0])
heights = [0] * cols
total = 0
for i in range(rows):
# Update heights for histogram
for j in range(cols):
if mat[i][j] == 1:
heights[j] += 1
else:
heights[j] = 0
total += self.countRectangles(heights)
return total
def countRectangles(self, heights: List[int]) -> int:
stack = []
count = 0
curr_sum = [0] * len(heights)
for i, h in enumerate(heights):
while stack and heights[stack[-1]] >= h:
stack.pop()
if stack:
prev_index = stack[-1]
curr_sum[i] = curr_sum[prev_index] + h * (i - prev_index)
else:
curr_sum[i] = h * (i + 1)
stack.append(i)
count += curr_sum[i]
return count