-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathfind-sorted-submatrices-with-maximum-element-at-most-k.py
58 lines (53 loc) · 1.82 KB
/
find-sorted-submatrices-with-maximum-element-at-most-k.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
57
58
# Time: O(m * n)
# Space: O(m)
# mono stack
class Solution(object):
def countSubmatrices(self, grid, k):
"""
:type grid: List[List[int]]
:type k: 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(grid)
for j in reversed(range(len(grid[0]))):
for i in xrange(len(grid)):
heights[i] = 0 if grid[i][j] > k else heights[i]+1 if j+1 < len(grid[0]) and grid[i][j] >= grid[i][j+1] else 1
result += count(heights)
return result
# Time: O(m * n)
# Space: O(m)
# mono stack, dp
class Solution2(object):
def countSubmatrices(self, grid, k):
"""
:type grid: List[List[int]]
:type k: 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(grid)
for j in reversed(range(len(grid[0]))):
for i in xrange(len(grid)):
heights[i] = 0 if grid[i][j] > k else heights[i]+1 if j+1 < len(grid[0]) and grid[i][j] >= grid[i][j+1] else 1
result += count(heights)
return result