You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
int minSubArrayLen(int target, vector<int>& nums) {
int left = 0, right = 0;
int sum = 0, size = INT_MAX;
while (right < nums.size()) {
sum += nums[right];
while (sum >= target) {
size = min(size, right - left + 1);
sum -= nums[left++];
}
++right;
}
return size == INT_MAX ? 0 : size;
}
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
vector<int> nums = {2, 3, 1, 2, 4, 3};
int target = 7;
Solution solve;
int result = solve.minSubArrayLen(target, nums);
cout << result << '\n';
return 0;
}
/*
------------------
Problem Statement:
------------------
Given an array of positive integers nums and a positive integer target, return the minimal length of a subarray whose sum is greater than or equal to target. If there is no such subarray, return 0 instead.
----------
Example 1:
----------
Input: target = 7, nums = [2,3,1,2,4,3]
Output: 2
Explanation: The subarray [4,3] has the minimal length under the problem constraint.