LeetCode 209. Minimum Size Subarray Sum

简介: 给定一个含有 n 个正整数的数组和一个正整数 s ,找出该数组中满足其和 ≥ s 的长度最小的连续子数组。如果不存在符合条件的连续子数组,返回 0。

v2-cd80b767ec2f91e1207866ec768e60a2_1440w.jpg

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.


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).


描述



给定一个含有 n 个正整数的数组和一个正整数 s ,找出该数组中满足其和 ≥ s 的长度最小的连续子数组。如果不存在符合条件的连续子数组,返回 0。


示例:

输入: s = 7, nums = [2,3,1,2,4,3]

输出: 2

解释: 子数组 [4,3] 是该条件下的长度最小的连续子数组。


进阶:

如果你已经完成了O(n) 时间复杂度的解法, 请尝试 O(n log n) 时间复杂度的解法。


思路



  • 使用两个指针left和right,用_sum来表示当前的和. right不断向右移动,_sum累加,当_sum大于等于s的时候,left向右移动直到_sum小于s,有效长度为right-left+2,返回所有有效长度的最小值即可.


# -*- coding: utf-8 -*-
# @Author:             何睿
# @Create Date:        2019-01-22 11:37:16
# @Last Modified by:   何睿
# @Last Modified time: 2019-01-22 11:37:16
import sys
class Solution:
    def minSubArrayLen(self, s, nums):
        """
        :type s: int
        :type nums: List[int]
        :rtype: int
        """
        # 初始化长度
        length = sys.maxsize
        _sum, left, right = 0, 0, 0
        while right < len(nums):
            _sum += nums[right]
            # 如果当前的和已经大于等于s
            if _sum >= s:
                # 我们将左指针向右移动,和小于s时跳出
                while _sum >= s and left <= right:
                    _sum -= nums[left]
                    left += 1
                # 更新长度
                length = min(right - left + 2, length)
            right += 1
        # 如果length始终没有发生改变返回0,否则返回length本身
        return length if length != sys.maxsize else 0


源代码文件在这里.


目录
相关文章
|
5月前
Leetcode 53.Maximum Subarray
题意简单,给出一个数组,求出其中最大的子数组和。 这种简单题目背后蕴藏着很巧妙的解题方法。其实只需要遍历一次数组就可以求得解。 思路是这样的,你想想看,如果一段子数组的和是负数, 那么这一段子数组不可能是最大和数组的一部分,丢掉重新从下一个位置开始选。
27 0
LeetCode 433. Minimum Genetic Mutation
现在给定3个参数 — start, end, bank,分别代表起始基因序列,目标基因序列及基因库,请找出能够使起始基因序列变化为目标基因序列所需的最少变化次数。如果无法实现目标变化,请返回 -1。
48 0
LeetCode 433. Minimum Genetic Mutation
|
存储 索引
LeetCode 310. Minimum Height Trees
对于一个具有树特征的无向图,我们可选择任何一个节点作为根。图因此可以成为树,在所有可能的树中,具有最小高度的树被称为最小高度树。给出这样的一个图,写出一个函数找到所有的最小高度树并返回他们的根节点。
51 0
LeetCode 310. Minimum Height Trees
LeetCode 152. Maximum Product Subarray
给定一个整数数组 nums ,找出一个序列中乘积最大的连续子序列(该序列至少包含一个数)。
31 0
LeetCode 152. Maximum Product Subarray
LeetCode 64. Minimum Path Sum
给定m x n网格填充非负数,找到从左上到右下的路径,这最小化了沿其路径的所有数字的总和。 注意:您只能在任何时间点向下或向右移动。
68 0
LeetCode 64. Minimum Path Sum
LeetCode 53. Maximum Subarray
给定整数数组nums,找到具有最大总和的子数组(数组要求连续)并且返回数组的和,给定的数组包含至少一个数字。
32 0
|
Python
[Leetcode][python]Maximum Subarray/最大子序和
题目大意 由 N 个整数元素组成的一维数组 (A[0], A[1],…,A[n-1], A[n]),这个数组有很多连续子数组,那么其中数组之和的最大值是什么呢? 子数组必须是连续的。 不需要返回子数组的具体位置。 数组中包含:正整数,零,负整数。
80 0
Leetcode-Medium 152. Maximum Product Subarray
Leetcode-Medium 152. Maximum Product Subarray
96 0
|
索引
LeetCode 599: 两个列表的最小索引总和 Minimum Index Sum of Two Lists
题目: 假设 Andy 和 Doris 想在晚餐时选择一家餐厅,并且他们都有一个表示最喜爱餐厅的列表,每个餐厅的名字用字符串表示。 Suppose Andy and Doris want to choose a restaurant for dinner, and they both have a list of favorite restaurants represented by strings. 你需要帮助他们用最少的索引和找出他们共同喜爱的餐厅。
849 0
|
Java 索引 Python
LeetCode 209:最小长度的子数组 Minimum Size Subarray Sum
给定一个含有 n 个正整数的数组和一个正整数 s ,找出该数组中满足其和 ≥ s 的长度最小的连续子数组。如果不存在符合条件的连续子数组,返回 0。
550 0