-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathcount-submatrices-with-all-ones.py
56 lines (51 loc) · 1.62 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
51
52
53
54
55
56
# Time: O(m * n)
# Space: O(n)
# mono stack
class Solution(object):
def numSubmat(self, mat):
"""
:type mat: List[List[int]]
:rtype: int
"""
def count(heights):
result = curr = 0
stk = []
for i in xrange(len(heights)):
while stk and heights[stk[-1]] >= heights[i]:
j = stk.pop()
curr -= (heights[j]-heights[i])*(j-(stk[-1] if stk else -1))
stk.append(i)
curr += heights[i]
result += curr
return result
result = 0
heights = [0]*len(mat[0])
for i in xrange(len(mat)):
for j in xrange(len(mat[0])):
heights[j] = heights[j]+1 if mat[i][j] == 1 else 0
result += count(heights)
return result
# Time: O(m * n)
# Space: O(n)
# mono stack, dp
class Solution2(object):
def numSubmat(self, mat):
"""
:type mat: List[List[int]]
:rtype: int
"""
def count(heights):
dp, stk = [0]*len(heights), []
for i in xrange(len(heights)):
while stk and heights[stk[-1]] >= heights[i]:
stk.pop()
dp[i] = dp[stk[-1]] + heights[i]*(i-stk[-1]) if stk else heights[i]*(i-(-1))
stk.append(i)
return sum(dp)
result = 0
heights = [0]*len(mat[0])
for i in xrange(len(mat)):
for j in xrange(len(mat[0])):
heights[j] = heights[j]+1 if mat[i][j] == 1 else 0
result += count(heights)
return result