209. Minimum Size Subarray Sum
problem description
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).algorithm thought
code
class Solution {
public:
int minSubArrayLen(int s, vector<int>& nums) {
if(nums.size()==0)
return 0;
int fast=0,slow=0;
int sum=0;
int res=INT_MAX;
while(fast<nums.size()){
sum+=nums[fast];
while(sum>=s){
res=min(res,fast-slow+1);
sum-=nums[slow++];
}
fast++;
}
return res==INT_MAX?0:res;
}
};algorithm analysis
Previous[208. Implement Trie (Prefix Tree)](208.-Implement-Trie-(Prefix-Tree).md)Next211. Add and Search Word Data structure design
Last updated