1 题目
Given an array of n positive integers and a positive integer s, find the minimal length of a contiguous subarray of which the sum ≥ s. If there isn't one, return 0 instead.
Example:
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).
2 标准解
O(n)
class Solution {
public:
int minSubArrayLen(int s, vector<int>& nums) {
int l = 0, r = 0, n = nums.size(), sum = 0, len = INT_MAX;
while (r < n) {
sum += nums[r++];
while (sum >= s) {
len = min(len, r - l);
sum -= nums[l++];
}
}
return len == INT_MAX ? 0 : len;
}
};
//O(nlogn)
class Solution {
public:
int minSubArrayLen(int s, vector<int>& nums) {
int n = nums.size(), len = INT_MAX;
vector<int> sums(n + 1, 0);
for (int i = 1; i <= n; i++) {
sums[i] = sums[i - 1] + nums[i - 1];
}
for (int i = n; i >= 0 && sums[i] >= s; i--) {
int j = upper_bound(sums.begin(), sums.end(), sums[i] - s) - sums.begin();
len = min(len, i - j + 1);
}
return len == INT_MAX ? 0 : len;
}
};