[LeetCode] 209. Minimum Size Subarray Sum 最短子数组之和
2021-06-22 11:03
标签:compare vat poi spl subarray coding break als minimal 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: 这道题的关键是由正整数组成的数组,这样才能保证累加的数组是递增的,才能使用双指针或者二分法。 解法1: 双指针,滑动窗口。 解法2: 二分法 Java: moving window Java: BS Java: BS Java: Java: Python: Python: C++: C++: C++: 二分法 C++: 类似题目: [LeetCode] 53. Maximum Subarray 最大子数组 [LeetCode] 560. Subarray Sum Equals K 子数组和为K [LeetCode] 209. Minimum Size Subarray Sum 最短子数组之和 标签:compare vat poi spl subarray coding break als minimal 原文地址:https://www.cnblogs.com/lightwindy/p/9678709.htmlInput:
s = 7, nums = [2,3,1,2,4,3]
Output: 2
Explanation: the subarray [4,3]
has the minimal length under the problem constraint.public class Solution {
public int minSubArrayLen(int s, int[] nums) {
int i = 0, j = 0, sum = 0, min = Integer.MAX_VALUE;
while (j =s){
while (sum >= s && i
public class Solution {
public int minSubArrayLen(int s, int[] nums) {
int i = 1, j = nums.length, min = 0;
while (i = size) sum -= nums[i - size];
sum += nums[i];
if (sum >= s) return true;
}
return false;
}
}
public class Solution {
public int minSubArrayLen(int s, int[] nums) {
int sum = 0, min = Integer.MAX_VALUE;
int[] sums = new int[nums.length];
for (int i = 0; i = s) j = m - 1;
else i = m + 1;
}
return i;
}
public int minSubArrayLen(int s, int[] a) {
if (a == null || a.length == 0)
return 0;
int i = 0, j = 0, sum = 0, min = Integer.MAX_VALUE;
while (j = s) {
min = Math.min(min, j - i);
sum -= a[i++];
}
}
return min == Integer.MAX_VALUE ? 0 : min;
}
public class Solution {
public int minSubArrayLen(int s, int[] nums) {
return solveNLogN(s, nums);
}
private int solveN(int s, int[] nums) {
int start = 0, end = 0, sum = 0, minLen = Integer.MAX_VALUE;
while (end = s) sum -= nums[start++];
if (end - start + 1 = key){
hi = mid - 1;
} else {
lo = mid + 1;
}
}
return lo;
}
}
# Sliding window solution.
class Solution:
# @param {integer} s
# @param {integer[]} nums
# @return {integer}
def minSubArrayLen(self, s, nums):
start = 0
sum = 0
min_size = float("inf")
for i in xrange(len(nums)):
sum += nums[i]
while sum >= s:
min_size = min(min_size, i - start + 1)
sum -= nums[start]
start += 1
return min_size if min_size != float("inf") else 0
# Time: O(nlogn)
# Space: O(n)
# Binary search solution.
class Solution2:
# @param {integer} s
# @param {integer[]} nums
# @return {integer}
def minSubArrayLen(self, s, nums):
min_size = float("inf")
sum_from_start = [n for n in nums]
for i in xrange(len(sum_from_start) - 1):
sum_from_start[i + 1] += sum_from_start[i]
for i in xrange(len(sum_from_start)):
end = self.binarySearch(lambda x, y: x
/ O(n)
class Solution {
public:
int minSubArrayLen(int s, vector
class Solution {
public:
int minSubArrayLen(int s, vector
// O(nlgn)
class Solution {
public:
int minSubArrayLen(int s, vector
class Solution {
public:
int minSubArrayLen(int s, vector
上一篇:常见前端算法面试题
下一篇:(转)python 判断数据类型
文章标题:[LeetCode] 209. Minimum Size Subarray Sum 最短子数组之和
文章链接:http://soscw.com/essay/97340.html