Medium
Minimum Size Subarray Sum
- Time limit
- 2 s
- Memory limit
- 256 MB
- Estimated
- 20 min
- Sliding Window
Description
Description
Given a positive integer target and an array of positive integers nums, find the length of the shortest contiguous subarray whose sum is greater than or equal to target. If no such subarray exists, return 0.
Examples
Example 1
- Input:
target = 7, nums = [2, 3, 1, 2, 4, 3] - Output:
2 - Explanation: The subarray
[4, 3]has length2and sum7.
Example 2
- Input:
target = 4, nums = [1, 4, 4] - Output:
1
Example 3
- Input:
target = 11, nums = [1, 1, 1, 1, 1, 1, 1, 1] - Output:
0
Expected Function Behavior
Implement minSubArrayLen(target, nums) and return the minimum length, or 0 if impossible.
Constraints
1 <= target <= 10000001 <= nums.length <= 1000001 <= nums[i] <= 10000
Sample cases
3Sample 1
Input
nums = [2,3,1,2,4,3] target = 7
Expected output
2
Sample 2
Input
nums = [1,4,4] target = 4
Expected output
1
Skills and languages
Patterns
- Sliding Window
Languages
- Python 3
Solve this problem
Create a free account to write, run and submit your solution.
- Write Python or Java in the browser, with your draft saved as you type
- Run your code on the examples as often as you like
- Submit to a sandboxed judge and see exactly which case failed
- Ask the AI mentor for a hint without being handed the answer