-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMinimum size subarray sum.cpp
More file actions
32 lines (31 loc) · 1.14 KB
/
Minimum size subarray sum.cpp
File metadata and controls
32 lines (31 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/*
Platform :- Leetcode
Problem :- minimize size subarray sum
Approach :- we traverse the array ans take two pointer both is initialised to (start = 0 & end = 0) in beginning as we move ahead we do ( tot + = nums[i] and end =i )
if the value is ( tot > target && (tot - nums[start] >target)) we do ( tot - = nums[start] ,start++) till the time ( start < end ) and our condition is satisfied
at the end if our ( tot>= target) we store the min value of ( (end -start)+1) in ans
*/
class Solution {
public:
int minSubArrayLen(int target, vector<int>& nums) {
int start=0,end=0;
int tot=0,ans=0;
for(int i=0;i<nums.size();++i){
tot+=nums[i];
end=i;
// cout<<tot<<" "<<end<<" ";
while(tot>target && start<end){
if((tot-nums[start])>=target){
tot-=nums[start];
start++;
}
else break;
}
if(tot>=target) {
if(ans==0)ans=(end-start)+1;
else ans=min(ans,(end-start)+1);
}
}
return ans;
}
};