Skip to content

Commit

Permalink
Create sum-of-subarray-minimums.py
Browse files Browse the repository at this point in the history
  • Loading branch information
kamyu104 authored Sep 16, 2018
1 parent 6ca484e commit 25a8371
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions Python/sum-of-subarray-minimums.py
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

0 comments on commit 25a8371

Please sign in to comment.