Lintcode Medium title: Minimum Size Subarray and smallest sub-array greater than s

Source: Internet
Author: User
Tags lintcode

Topic

and the smallest sub-array greater than S

Given an array of n integers and a positive integer s, find the minimum length subarray in the array that satisfies its and ≥s. If there is no solution, return-1.

Sample Example

Given an array [2,3,1,2,4,3] and S = 7 , the sub-array is the [4,3] minimum length sub-array under that condition.

challenges

If you have completed programming O (N) time complexity, try the O (n log n) time complexity again.

Solving

Define two pointers, slow,fast, and go right at a speed

Fast first finds the first Yes Sum>s value

Calculates the length of the current sub-array based on fast and slow

Sum-=nums[slow], looking for the last slow that does not satisfy the sum>=s, updates the minimum value of the subarray length each time.

Here is generally asked what does not have an array of sorting problems, where the arrays are positive, if the negative is a problem, how to add, the sum value is increased, the order of at least affect the speed of the problem, and we go forward to the value of the sum>=s, but also to the back of the slow part of the check. This explanation is not very sufficient, I do not know the specific.

Also online to say time complexity is O (N), how I feel time complexity should be very close to O (N2), do not know why?

Action as shown

 Public classSolution {/**     * @paramnums:an array of integers *@paramS:an Integer *@return: An integer representing the minimum size of Subarray*/     Public intMinimumSize (int[] Nums,ints) {//Write your code here        if(Nums.length = = 0 | | nums = =NULL)            return-1; intSlow = 0; intFast = 0; intres =Integer.max_value; intsum = 0;  while(Fast <nums.length) {             while(Fast < Nums.length && Sum <s) {Sum+=Nums[fast]; Fast+ = 1; //System.out.println (sum+ "" +fast);            }            //System.out.println ();             while(sum>=s) {Res= Math.min (Res,fast-slow); Sum-=Nums[slow]; Slow+=1; }        }        returnRes==integer.max_value?-1: Res; }}
Java Code

Total time: 1910 Ms

classSolution:#@param nums:a List of integers     #@param s:an Integer     #@return: An integer representing the minimum size of Subarray    defminimumsize (self, nums, s):#Write your code here        ifs = = NoneorLen (nums) = =0:return-1; Lens=len (nums) Slow=0 Fast=0 Sum=0 Res= Lens+1 whileFast <Lens: whileFast < Lens andSum <S:sum+=Nums[fast] Fast+=1 whileSlow < fast andsum>=S:res= Min (Res,fast-slow) sum-=Nums[slow] Slow+=1ifRes ==lens+1:            return-1Else:            returnRes
Python Code

Total time: 444 Ms

Lintcode Medium title: Minimum Size Subarray and smallest sub-array greater than s

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.