Given an array of integers arr
, find the sum of min(b)
, where b
ranges over every (contiguous) subarray of arr
. Since the answer may be large, return the answer modulo 10^9 + 7
.
arr
?arr
?10^9 + 7
?PLE
and NLE
where:
PLE[i]
represents the number of contiguous subarrays ending at arr[i]
where arr[i]
is the minimum.NLE[i]
represents the number of contiguous subarrays starting at arr[i]
where arr[i]
is the minimum.arr
, the count of subarrays where the element is minimum is PLE[i] * NLE[i]
.10^9 + 7
.def sumSubarrayMins(arr):
MOD = 10**9 + 7
n = len(arr)
# Arrays to store previous less and next less
PLE = [0] * n
NLE = [0] * n
# Monotonic stack for Previous Less Element (PLE)
stack = []
for i in range(n):
while stack and arr[stack[-1]] >= arr[i]:
stack.pop()
PLE[i] = i + 1 if not stack else i - stack[-1]
stack.append(i)
# Clear stack for reuse
stack = []
# Monotonic stack for Next Less Element (NLE)
for i in range(n-1, -1, -1):
while stack and arr[stack[-1]] > arr[i]:
stack.pop()
NLE[i] = n - i if not stack else stack[-1] - i
stack.append(i)
# Calculate the result
result = 0
for i in range(n):
result = (result + arr[i] * PLE[i] * NLE[i]) % MOD
return result
# Example Usage:
arr = [3, 1, 2, 4]
print(sumSubarrayMins(arr)) # Output: 17
By exploiting the properties of the stack and the arrays PLE
and NLE
, we can efficiently compute the result while avoiding the brute-force O(n^3) approach.
Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?