209. Minimum Size Subarray Sum

https://leetcode.com/problems/minimum-size-subarray-sum/#/description

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.

For example, given the array [2,3,1,2,4,3] and s = 7,
the subarray [4,3] has the minimal length under the problem constraint.

Sol:

https://discuss.leetcode.com/topic/13759/python-3-different-ac-solutions

class Solution(object):
    def minSubArrayLen(self, s, nums):
        """
        :type s: int
        :type nums: List[int]
        :rtype: int
        """

        # two pointers
        if len(nums) == 0:
            return 0

        sum = 0
        i = 0
        # fast pointer j
        j = 0
        res = len(nums) + 1
        while sum < s:
            sum += nums[j]
            j += 1

            # to enter the while loop below, sum is greater than s, now let‘s use another pointer i(slow) pointer to find the intervals between i and j.  

            while sum >= s:
                # if i and j still have elements among them
                if  res > j - i:
                    res = j - i
                sum -= nums[i]
                i += 1

            if j == len(nums):
                break

        # if sum > s at the very beginning, then the program will not enter the first while loop. Thus 0 will be returned.
        if res > len(nums):
            return 0

        return res
            
时间: 2024-08-23 13:42:21

209. Minimum Size Subarray Sum的相关文章

【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] 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 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 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];

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开头的最短sub

[email&#160;protected] [209]Minimum Size Subarray Sum

https://leetcode.com/problems/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

[leedcode 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

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