-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp907.py
More file actions
66 lines (56 loc) · 1.86 KB
/
p907.py
File metadata and controls
66 lines (56 loc) · 1.86 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
class Solution(object):
def sumSubarrayMins(self, A):
"""
:type A: List[int]
:rtype: int
"""
left = [None] * len(A)
right = [None] * len(A)
stack = []
for i in xrange(0, len(A)):
while stack and A[i] <= A[stack[-1]]:
stack.pop(0)
if stack:
left[i] = stack[-1]
else:
left[i] = -1
stack.append(i)
stack = []
for i in xrange(len(A)-1, -1 , -1):
while stack and A[i] < A[stack[-1]]:
stack.pop()
if stack:
right[i] = stack[-1]
else:
right[i] = len(A)
stack.append(i)
total = sum([A[i] * (i - left[i]) * (right[i] - i) for i in xrange(len(A))]) % (10 ** 9 + 7)
print left,right
return total
def sumSubarrayMins2(self, A):
MOD = 10**9 + 7
N = len(A)
# prev has i* - 1 in increasing order of A[i* - 1]
# where i* is the answer to query j
stack = []
prev = [None] * N
for i in xrange(N):
while stack and A[i] <= A[stack[-1]]:
stack.pop()
prev[i] = stack[-1] if stack else -1
stack.append(i)
# next has k* + 1 in increasing order of A[k* + 1]
# where k* is the answer to query j
stack = []
next_ = [None] * N
for k in xrange(N-1, -1, -1):
while stack and A[k] < A[stack[-1]]:
stack.pop()
next_[k] = stack[-1] if stack else N
stack.append(k)
print prev,next_
# Use prev/next array to count answer
return [(i - prev[i]) * (next_[i] - i) * A[i]
for i in xrange(N)]
s = Solution()
print(s.sumSubarrayMins([85,93,93,90]))