Stretches With a Sum in Range
All items of nums are positive. Count the contiguous, non-empty stretches of nums whose sum is at least lo and at most hi.
Stretches at different positions count separately even when they hold the same values. Because every item is positive, the number of stretches with a sum at most some limit can be found with one window that grows on the right and shrinks on the left; the answer is that count for hi minus that count for lo - 1.
Example 1
nums [1,2,3,4]
lo 3
hi 6
returns 5
The stretches [3], [4], [1, 2], [2, 3] and [1, 2, 3] have sums 3, 4, 3, 5 and 6; the other five sums are 1, 2, 7, 9 and 10.
Example 2
nums [2,2,2]
lo 4
hi 4
returns 2
Only the two stretches [2, 2] have sum exactly 4.
Constraints
0 <= len(nums) <= 1001 <= nums[i] <= 1001 <= lo <= hi <= 10000
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.