Minimum Size Subarray Sum
Medium Sliding Window AI round
nums is a list of positive whole numbers, and target is a positive whole number.
A subarray is a run of one or more numbers that sit next to each other in nums. Find the shortest subarray whose numbers add up to target or more.
Return the length of that subarray, or 0 if there is none: when even the whole list adds up to less than target.
Example 1
2 + 6 + 3 = 11 reaches the target with three numbers next to each other. No two neighbours add up to 10 or more (the most is 6 + 3 = 9), so nothing shorter works.
Example 2
All four numbers add up to 17, less than 20, so no subarray reaches the target.
Example 3
Constraints
1 ≤ nums.length ≤ 2 × 10⁵1 ≤ nums[i] ≤ 50001 ≤ target ≤ 10⁹
Practice this in an AI round
30 minutes on this problem with an AI agent beside your code, then a report on your solution and how you used the agent.
Your first AI round is free.
Watch the explanation
More Sliding Window
- Longest Substring Without Repeating CharactersAI round · Medium
- Sliding Window MaximumAI round · Hard
- Minimum Window SubstringAI round · Hard
- Longest Substring with At Most K Distinct CharactersMedium
- Longest Repeating Character ReplacementMedium