Leetcode 209. Minimum Size Subarray Sum

思路一: 本题nums里面的数由于都是正数,所以可以用两个指针 start 和 end = 0。end++向后寻找,直到 start和end之间的sum>=s。例如 nums = [2, 3, 1, 2, 4, 3], s = 7。这一步结束时找到了start = 0 的最短的满足条件的subarray。

显然当前的这个解可能不是以end = 3的最优解,所以start++,直到total < 7。

这时我们又需要end++,直到total>=s来寻找以start = 1开头的最短subarray。

 1 class Solution(object):
 2     def minSubArrayLen(self, s, nums):
 3         """
 4         :type s: int
 5         :type nums: List[int]
 6         :rtype: int
 7         """
 8         size = len(nums)
 9         if size == 0:
10             return 0
11         left = right = 0
12         total = 0
13         res = size + 1
14
15         while right < size:
16             while total < s and right < size:
17                 total += nums[right]
18                 right += 1
19             while left < right and total >= s:
20                 res = min(res, right - left)
21                 total -= nums[left]
22                 left += 1
23
24         return res if res <= size else 0
时间: 2024-10-24 14:59:03

Leetcode 209. Minimum Size Subarray Sum的相关文章

[LeetCode] 209. Minimum Size Subarray Sum 最短子数组之和

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 subarr

LeetCode 209. Minimum Size Subarray Sum (O(n)实现)

动态规划: len[i]: - 若存在begin使得sum(nums.begin()+begin, nums.begin()+i+1)>=s且sum(nums.begin()+begin-1, nums.begin()+i+1)<s,那么len[i] = i - begin + 1 (这串数字的长度) - 反之,len[i] = len[i - 1] + 1; sum[i]: - 若存在上述begin, sum = sum(nums.begin()+begin, nums.begin()+i+

LeetCode 209. Minimum Size Subarray Sum(DP)

题目 题意:求一个数组里最短的连续子序列的和大于等于s的长度 题解:可以用动态规划,我就是用动态规划过的,但是确实不是最简单的解法,看了题解最简单的是双指针, 双指针 class Solution { public: int minSubArrayLen(int s, vector<int>& nums) { int sum=0; int left=0; int ans=INT_MAX; for(int i=0;i<nums.size();i++) { sum+=nums[i];

Java for LeetCode 209 Minimum Size Subarray Sum

Given an array of n positive integers and a positive integer s, find the minimal length of a subarray of which the sum ≥ s. If there isn't one, return 0 instead. For example, given the array [2,3,1,2,4,3] and s = 7, the subarray [4,3] has the minimal

LeetCode 209 Minimum Size Subarray Sum 最小子数组和问题

Given an array of n positive integers and a positive integer s, find the minimal length of a contiguoussubarray 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 subarra

[刷题] LeetCode 209 Minimum Size Subarray Sum

要求 给定一个含有 n 个正整数的数组和一个正整数 s 找出该数组中满足其和 ≥ s 的长度最小的连续子数组 如果不存在符合条件的连续子数组,返回 0 示例 输入:s = 7, nums = [2,3,1,2,4,3] 输出:2 解释:子数组 [4,3] 是该条件下的长度最小的连续子数组 思路 暴力解法(n3) 滑动窗口() 1 class Solution{ 2 public: 3 int minSubArrayLen(int s, vector<int>& nums){ 4 int

【LeetCode】209. Minimum Size Subarray Sum

Minimum Size Subarray Sum Given an array of n positive integers and a positive integer s, find the minimal length of a subarray of which the sum ≥ s. If there isn't one, return 0 instead. For example, given the array [2,3,1,2,4,3] and s = 7,the subar

LeetCode OJ 209. Minimum Size Subarray Sum

Given an array of n positive integers and a positive integer s, find the minimal length of a subarray of which the sum ≥ s. If there isn't one, return 0 instead. For example, given the array [2,3,1,2,4,3] and s = 7,the subarray [4,3] has the minimal

【leetcode】Minimum Size Subarray Sum(middle)

Given an array of n positive integers and a positive integer s, find the minimal length of a subarray of which the sum ≥ s. If there isn't one, return 0 instead. For example, given the array [2,3,1,2,4,3] and s = 7,the subarray [4,3] has the minimal