Given an array of positive integers nums and a positive integer target, return the minimal length of a subarray whose sum is greater than or equal to target. If there is no such subarray, return 0 instead.
1 ≤ target ≤ 10⁹1 ≤ nums.length ≤ 10⁵1 ≤ nums[i] ≤ 10⁴Use two pointers to define a window.
A subarray is a contiguous run, so it is fully described by where it starts and where it ends. The plain way is to try every start and walk right until the sum reaches target. That is up to n²/2 additions, and at n = 10⁵ that's about 5 billion.
Time O(n)Space O(1)
The full walkthrough and code are part of Premium
2