155. Shortest Enough Window
A bakery logs how many loaves it sells each day in the array nums. The owner wants to find the shortest run of consecutive days in which the bakery sold at least target loaves in total, so that a promotion can be judged on its quickest success.
Return the length of the shortest contiguous subarray of nums whose sum is greater than or equal to target. If no subarray reaches the target, return 0. Every daily count is positive, and the total of the whole array always fits in a 32-bit signed integer.
Example 1
- Input:
- nums = [5,1,3,5,10,7,4,9,2,8], target = 15
- Output:
- 2
- Explanation:
[5,10] sums to 15 and has length 2. No single element reaches 15, so 2 is the shortest.
Example 2
- Input:
- nums = [1,1,1], target = 10
- Output:
- 0
- Explanation:
The whole array sums to only 3, so no window reaches 10 and the answer is 0.
Example 3
- Input:
- nums = [8], target = 8
- Output:
- 1
- Explanation:
The single element already meets the target, so the answer is 1.
Constraints
1 ≤ nums.length ≤ 105
1 ≤ nums[i] ≤ 104
1 ≤ target ≤ 109
How this problem is judged
- Answers
- Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
- Time per case
- Python 1,200 msC++ 300 msJava 600 msJavaScript 600 msTypeScript 600 ms
Expected complexity
- Time
- O(n)
- Space
- O(1)