Loading...
Loading...
Loading...
Take every non-empty run of neighbouring numbers in a list, find its smallest number, and add up all of these smallest numbers. The sum can be large, so print it modulo 1000000007.
Example 1
Input
4 3 1 2 4
Output
17
The runs have minimums 3, 1, 2, 4, 1, 1, 2, 1, 1 and 1. The sum is 17.
Example 2
Input
2 2 2
Output
6
Try the problem first. Open one hint at a time.
Count for each number how many runs have it as their minimum.
Use a monotonic stack to find how far it can stretch left and right. Break ties on one side only.
Write your Python code, run it on the examples and submit it to hidden tests.
Solve in the editor