All problems

Minimum Size Subarray Sum

Medium · sliding window

A shop records its takings for each day in a list nums. Every entry is a positive whole number. You are also given a whole number target, the amount the owner hopes to take.

Find the shortest run of consecutive days whose takings add up to at least target, and return its length.

If no run reaches target, even the whole list, return 0.

Examples

Example 1

Input: nums = [3, 1, 4, 1, 5], target = 9

Output: 3

The run [4, 1, 5] adds up to 10. No run of two days reaches 9.

Example 2

Input: nums = [6, 2, 8], target = 7

Output: 1

The single day with 8 is already enough.

Example 3

Input: nums = [1, 1, 1], target = 10

Output: 0

All three days together only make 3.

Constraints