Formatted question description: https://leetcode.ca/all/209.html
Given an array of n positive integers and a positive integer s, find the minimal length of a contiguous subarray of which the sum ≥ s. If there isn't one, return 0 instead. Example: Input: s = 7, nums = [2,3,1,2,4,3] Output: 2 Explanation: the subarray [4,3] has the minimal length under the problem constraint. Follow up: If you have figured out the O(n) solution, try coding another solution of which the time complexity is O(n log n). @tag-array
Note that this is greater than or equal to, not equal to.
Define two pointers left and right to record the left and right boundary positions of the sub-array respectively, and then move right to the right,
- Until the sub-array sum is greater than or equal to the target value
- Or right reaches the end of the array, At this point, update the shortest distance, and move the left image to the right by one, and then subtract the removed value from the sum, and then repeat the above steps until right reaches the end and left reaches the critical position (that is, either reaches the boundary or again move to the right), and the sum will be checked.