InferenceAI

Enter at least two characters to search.

Start free

Subarray Minimums Total

hardStack40 min50 XP

Return the sum of min⁡(sub)\min(\text{sub}) over every contiguous subarray of nums, modulo 109+710^9 + 7.

Requirements

  • O(n)O(n): count how many subarrays each element is the minimum of, using monotonic stacks for its previous-smaller and next-smaller-or-equal positions.

Examples

EXAMPLE WALKTHROUGHclassic

A guided view of the published example, not a trace of your code.

INPUT[[3,1,2,4]]
TASKsubarray_minimums_total()
EXPECTED OUTPUT17

Start with the input values shown in this example.

Example 1 of 3

Input: nums = [3,1,2,4]

Output: 17

Explanation: classic

Input: nums = [11,81,94,43,3]

Output: 444

Explanation: larger

Input: nums = [1]

Output: 1

Explanation: single

Hint 1

If element i is the minimum for L choices of left end and R choices of right end, it contributes nums[i] * L * R.

Hint 2

Use strict on one side and non-strict on the other so equal values are counted once.

Topics

DSAStackPython
Subarray Minimums Total: Stack Coding Problem | InferenceAI