Leetcode 209
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.
===========
Given an integer array int a[] = { 2,3,1,2,4,3
}, given an integer number s;
The length of the smallest subarray in a neutron array and greater than or equal to s, otherwise 0;
An algorithm of time complexity O (n).
classa{ Public: intMinsubarraylen (intS, vector<int>&nums) { intStart =0; intMinlen =Int_max; intTsum =0; for(inti =0;i< (int) nums.size (); i++) {Tsum+=Nums[i]; while(Tsum >=s) {Minlen= Min (minlen,i-start+1); Tsum= tsum-nums[start++];///The key is this step, when the tsum>=3 is, start subscript is going forward, and I subscript is the same}}returnMinlen==int_max?0: Minlen; }};
A subarray greater than or equal to the minimum length of a given value minimum size subarray sum [209]