Shortest Stretch Reaching a Total
nums may hold negative numbers. Return the length of the shortest stretch of nums (one or more items next to each other) whose sum is at least target, or -1 if no stretch reaches it.
Example 1
nums [4,-2,3,1]
target 5
returns 3
4 - 2 + 3 = 5 takes three items. No single item and no two neighbors reach 5.
Example 2
nums [1,2]
target 4
returns -1
Everything together is only 3.
Example 3
nums [6,-9,6]
target 6
returns 1
A single 6 is enough.
Constraints
1 <= len(nums) <= 300-105 <= nums[i] <= 1051 <= target <= 10**9
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.
More Two pointers problems
- Shortest Stretch Spelling It in OrderHard
- Shortest Stretch With Enough of Each LetterHard
- Stones Into a RowHard
- Stretches Between Two BoundsHard
- Busy WindowsBeginner
- Change Over k DaysBeginner
- Common StartBeginner
- Fixes to Make a MirrorBeginner