forked from shuboc/LeetCode-2
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
49 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# Time: O(n) | ||
# Space: O(n) | ||
|
||
# Given an array of integers A, find the sum of min(B), | ||
# where B ranges over every (contiguous) subarray of A. | ||
# | ||
# Since the answer may be large, return the answer modulo 10^9 + 7. | ||
# | ||
# Example 1: | ||
# | ||
# Input: [3,1,2,4] | ||
# Output: 17 | ||
# Explanation: Subarrays are [3], [1], [2], [4], [3,1], | ||
# [1,2], [2,4], [3,1,2], [1,2,4], [3,1,2,4]. | ||
# Minimums are 3, 1, 2, 4, 1, 1, 2, 1, 1, 1. Sum is 17. | ||
# | ||
# Note: | ||
# - 1 <= A.length <= 30000 | ||
# - 1 <= A[i] <= 30000 | ||
|
||
import itertools | ||
|
||
|
||
# Ascending stack solution | ||
class Solution(object): | ||
def sumSubarrayMins(self, A): | ||
""" | ||
:type A: List[int] | ||
:rtype: int | ||
""" | ||
M = 10**9 + 7 | ||
|
||
left, s1 = [0]*len(A), [] | ||
for i in xrange(len(A)): | ||
count = 1 | ||
while s1 and s1[-1][0] > A[i]: | ||
count += s1.pop()[1] | ||
left[i] = count | ||
s1.append([A[i], count]) | ||
|
||
right, s2 = [0]*len(A), [] | ||
for i in reversed(xrange(len(A))): | ||
count = 1 | ||
while s2 and s2[-1][0] >= A[i]: | ||
count += s2.pop()[1] | ||
right[i] = count | ||
s2.append([A[i], count]) | ||
|
||
return sum(a*l*r for a, l, r in itertools.izip(A, left, right)) % M |