MCPcopy Index your code
hub / github.com/HuberTRoy/leetCode / sumSubarrayMins

Method sumSubarrayMins

Array/SumOfSubarrayMinimums.py:113–185  ·  view source on GitHub ↗

:type A: List[int] :rtype: int

(self, A)

Source from the content-addressed store, hash-verified

111class Solution(object):
112
113 def sumSubarrayMins(self, A):
114 """
115 :type A: List[int]
116 :rtype: int
117 """
118 mod = 10**9 + 7
119
120 # count
121 left = []
122 right = []
123
124 # (num, count)
125 _left = []
126 _right = []
127 for i in A:
128 base = 1
129 while _left:
130 if i < _left[-1][0]:
131 base += _left[-1][1]
132 _left.pop()
133 else:
134 break
135
136 _left.append((i, base))
137 left.append(base)
138
139 for i in reversed(A):
140 base = 1
141 while _right:
142 if i <= _right[-1][0]:
143 base += _right[-1][1]
144 _right.pop()
145 else:
146 break
147
148 _right.append((i, base))
149 right.append(base)
150
151 right.reverse()
152
153 result = 0
154
155 for i in range(len(A)):
156 result += A[i] * left[i] * right[i]
157
158 return result % mod
159
160 # result = 0
161 # length = len(A)
162
163 # for i in range(length):
164 # _self = 1
165 # left = 0
166 # right = 0
167
168 # for j in range(i-1, -1, -1):
169 # if A[i] < A[j]:
170 # left += 1

Callers

nothing calls this directly

Calls 2

reverseMethod · 0.80
popMethod · 0.45

Tested by

no test coverage detected