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:
1 2 3
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})$.
Analysis
Brute-Force
Note: Be careful of the initialization.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
publicintminSubArrayLen(int s, int[] nums) { // assume nums is not null intn= nums.length; intminLen= Integer.MAX_VALUE; for (inti=0; i < n; ++i) { intsum=0; for (intj= i; j < n; ++j) { sum += nums[j]; if (sum >= s) { minLen = Math.min(minLen, j - i + 1); } } } return minLen == Integer.MAX_VALUE ? 0 : minLen; }
Based on the idea in 325. Maximum Size Subarray Sum Equals K, we can use a tree map for solving this problem.
Modification:
When we have duplicate prefix sums, we update the index. It is because in this problem we want the minimum size. However, since the problem statement says there are all positive integers, there won’t be duplicate prefix sums.
1 2 3 4
x s -----> ------> -------------> sum
Also, in this problem we want the sum greater than or equal to s, so we cannot use hash map. Instead, we use a tree map. In the example above, when we have a prefix sum sum, we check if there is an x such that sum - x >= s. In other words, we want x <= sum - s. It is to find the greatest x that satisfies the equation. This can be achieved by using floorKey(K) in TreeMap.
publicintminSubArrayLen(int s, int[] nums) { // assume nums is not null intn= nums.length; TreeMap<Integer, Integer> map = newTreeMap<>(); // <prefixSum, index> map.put(0, -1); intminLen= Integer.MAX_VALUE; intsum=0; for (inti=0; i < n; ++i) { sum += nums[i]; if (map.floorKey(sum - s) != null) { intsize= i - map.get(map.floorKey(sum - s)); // or floorEntry(K).getValue() minLen = Math.min(minLen, size); } map.put(sum, i); // update to the latest index } return minLen == Integer.MAX_VALUE ? 0 : minLen; }
Time: $O(N\log{N})$ Space: $O(N)$
Binary Search
Based on the same idea, we can implement the binary search by ourselves. We don’t need a tree map here because we have positive elements such that prefix sums are always increasing; in other words, as we build the array, it is always sorted in ascending order.
// To find the one (<= key) // we use upper-bound BS to find (> key) // exists: lo - 1 --> existing item // not exists: lo - 1 --> greatest item < key privateintbinarySearch(int[] nums, int lo, int hi, int key) { intoldLo= lo; intoldHi= hi; while (lo <= hi) { intmid= lo + (hi - lo) / 2; if (nums[mid] > key) { hi = mid - 1; } else { lo = mid + 1; } } lo -= 1; if (lo >= oldLo && lo <= oldHi) return lo; elsereturn -1; }
Time: $O(N\log{N})$ Space: $O(N)$
Two Pointers
1
s = 0, nums = [0] // this is an invalid input
Instead of having the starting index fixed, we update it when it no longer contributes to a solution.
1 2 3 4 5 6 7 8 9 10 11 12 13
s = 7, nums = [2, 3, 1, 2, 4, 3] // output: 2 i/j (2) // i = start, j = end i j(5) i j(6) i j(8)// increasing j does not give us a better solution i j(6) i j(10) i j(7) i j(6) i j(9) i j(7) i/j (3)
publicintminSubArrayLen(int s, int[] nums) { // assume nums is not null intn= nums.length; if (n == 0) { return0; } intsum= nums[0]; intminLen= Integer.MAX_VALUE; intstart=0, end = 0; // [start, end]
while (end < n) { if (sum < s) { ++end; if (end == n) break; sum += nums[end]; } else { // sum >= s minLen = Math.min(minLen, end - start + 1); sum -= nums[start]; ++start; } }