Given an array of positive integers nums
and a positive integer target
, return the minimal length of a contiguous subarray [numsl, numsl+1, ..., numsr-1, numsr]
of which the 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.
Example 2:
Input: target = 4, nums = [1,4,4] Output: 1
Example 3:
Input: target = 11, nums = [1,1,1,1,1,1,1,1] Output: 0
Constraints:
1 <= target <= 109
1 <= nums.length <= 105
1 <= nums[i] <= 105
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))
.
Presum and binary search.
class Solution:
def minSubArrayLen(self, target: int, nums: List[int]) -> int:
n = len(nums)
pre_sum = [0] * (n + 1)
for i in range(1, n + 1):
pre_sum[i] = pre_sum[i - 1] + nums[i - 1]
res = n + 1
for i in range(1, n + 1):
t = pre_sum[i - 1] + target
left, right = 0, n
while left < right:
mid = (left + right) >> 1
if pre_sum[mid] >= t:
right = mid
else:
left = mid + 1
if pre_sum[left] - pre_sum[i - 1] >= target:
res = min(res, left - i + 1)
return 0 if res == n + 1 else res
Slide window.
class Solution:
def minSubArrayLen(self, target: int, nums: List[int]) -> int:
n = len(nums)
left = right = 0
sum, res = 0, n + 1
while right < n:
sum += nums[right]
while sum >= target:
res = min(res, right - left + 1)
sum -= nums[left]
left += 1
right += 1
return 0 if res == n + 1 else res
Presum and binary search.
class Solution {
public int minSubArrayLen(int target, int[] nums) {
int n = nums.length;
int[] preSum = new int[n + 1];
for (int i = 1; i <= n; ++i) {
preSum[i] = preSum[i - 1] +nums[i - 1];
}
int res = n + 1;
for (int i = 1; i <= n; ++i) {
int t = preSum[i - 1] + target;
int left = 0, right = n;
while (left < right) {
int mid = (left + right) >> 1;
if (preSum[mid] >= t) {
right = mid;
} else {
left = mid + 1;
}
}
if (preSum[left] - preSum[i - 1] >= target) {
res = Math.min(res, left - i + 1);
}
}
return res == n + 1 ? 0 : res;
}
}
Slide window.
class Solution {
public int minSubArrayLen(int target, int[] nums) {
int n = nums.length;
int left = 0, right = 0;
int sum = 0, res = n + 1;
while (right < n) {
sum += nums[right];
while (sum >= target) {
res = Math.min(res, right - left + 1);
sum -= nums[left++];
}
++right;
}
return res == n + 1 ? 0 : res;
}
}
class Solution {
public:
int minSubArrayLen(int target, vector<int>& nums) {
int left = 0, right;
int sum = 0;
int minlen = INT_MAX;
for (right = 0; right < nums.size(); right++) {
sum += nums[right];
while (left <= right && sum >= target) {
minlen = min(minlen, right - left + 1);
sum -= nums[left++];
}
}
return minlen == INT_MAX ? 0 : minlen;
}
};
public class Solution {
public int MinSubArrayLen(int target, int[] nums) {
int n = nums.Length;
int left = 0, right = 0;
int sum = 0, res = n + 1;
while (right < n)
{
sum += nums[right];
while (sum >= target)
{
res = Math.Min(res, right - left + 1);
sum -= nums[left++];
}
++right;
}
return res == n + 1 ? 0 : res;
}
}