Given an array of n positive integers and a positive integer s, find the minimal length of a subarray of which the Sum≥s . If there isn ' t one, return 0 instead.
For example, given the array [2,3,1,2,4,3]
and s = 7
,
The subarray has the [4,3]
minimal length under the problem constraint.
Click to show more practice.
More Practice:
If you had figured out the O(n) solution, try coding another solution of which the time complexity was C5>o(n log n).
Credits:
Special thanks to @Freezen for adding this problem and creating all test cases.
"Problem Analysis"
Given an array of n positive numbers, a target value s, find the length of the shortest subarray greater than or equal to the target value, and return 0 if it does not exist.
Ideas
1. Time Complexity of O (n)
In order to find the smallest subarray, and the sum value of the subarray element is greater than or equal to s, we will find the starting and ending nodes of the subarray. Suppose we have found a sub-array that satisfies the condition, what should we do next?
To make the length of the array the shortest, we move the starting point backwards, and continue to move backwards if the condition is met. If the condition is not satisfied, we move the terminating node backwards and repeat the process above. Here's an example: Nums: [2,3,1,2,4,3],s=7.
(1) begin=0, end=0, sum=2,minlen=nums.length+1; At this point the sum<7,end moves backwards.
(2) Sum=5,end continue to move backwards.
(3) Sum=6,end continue to move backwards.
(4) Sum=8,begin move backwards, minlen=4.
(5) sum=6,end move backwards.
(6) Sum=9,begin move backwards, minlen=4.
(7) Sum=7,begin continue to move backwards, minlen=3.
(8) sum=6,end move backwards.
(9) Sum=9,begin move backwards, minlen=3.
(ten) The sum=7,begin moves backwards, minlen=2.
(11) End, return 2.
2. Time complexity of No (LOGN)
As to Nlogn solution, Logn immediately reminds you of binary search. In this case, you cannot sort as the current order actually matters. How does one get a ordered array then? Since all elements is positive, the cumulative sum must be strictly increasing. Then, a subarray sum can expressed as the difference between the sum of the cumulative. Hence, given a start index for the cumulative sum array, the other end index can is searched using binary search.
Because all the elements in the array are positive, the sum of the values for the backward summation is definitely incremented. The sum of a subarray and the difference that can be represented as an accumulation of two different positions in the array. So we construct an array of array sums and then use the binary lookup to solve the problem.
such as nums:[2,3,1,2,4,3],s=7. Accumulation and array sums:[0,2,5,6,8,12,15].
We traverse the sums array, for each element sum[i], find the first element that is larger than sums[i]+s, and then calculate the length of the Minlen at this time. Returns the smallest possible.
"Java Code 1" O (N)
1 Public classSolution {2 Public intMinsubarraylen (intSint[] nums) {3 if(Nums = =NULL|| Nums.length = = 0)return0;4 5 intBegin = 0, end = 0;6 intsum = nums[0], Minlen = nums.length + 1;7 8 while(End <nums.length) {9 if(Sum <s) {Ten if(End < Nums.length-1) OneSum + = nums[++end]; A Else Break; - } - Else{ theMinlen = Math.min (Minlen, end-begin+1); -sum = sum-nums[begin++]; - } - } + - returnMinlen <= nums.length? minlen:0; + } A}
"Java Code 1" NO (LOGN)
1 Public classSolution {2 Public intMinsubarraylen (intSint[] nums) {3 int[] sums =New int[Nums.length + 1];4 for(inti = 1; i < sums.length; i++) Sums[i] = sums[i-1] + nums[i-1];5 intMinlen =Integer.max_value;6 for(inti = 0; i < sums.length; i++) {7 intEnd = BinarySearch (i + 1, sums.length-1, Sums[i] +s, sums);8 if(end = = sums.length) Break;9 if(End-i < Minlen) Minlen = end-i;Ten } One returnMinlen = = Integer.max_value? 0: Minlen; A } - - Private intBinarySearch (intLointHiintKeyint[] sums) { the while(Lo <=hi) { - intMid = (lo + hi)/2; - if(Sums[mid] >=key) { -hi = mid-1; +}Else { -Lo = mid + 1; + } A } at returnLo; - } -}
Leetcode OJ 209. Minimum Size Subarray Sum