资讯专栏INFORMATION COLUMN

LeetCode 209:最小长度的子数组 Minimum Size Subarray Sum

Vixb / 660人阅读

摘要:如果不存在符合条件的连续子数组,返回。示例输入输出解释子数组是该条件下的长度最小的连续子数组。截取从索引到索引的数组,该数组之和若小于,则继续后移,直到大于等于。记录与差值返回的目标数。之后后移一位继续刷新新数组。

算法是一个程序的灵魂

公众号:爱写bug(ID:icodebugs)

作者:爱写bug

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

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.

示例:

输入: s = 7, nums = [2,3,1,2,4,3]
输出: 2
解释: 子数组 [4,3] 是该条件下的长度最小的连续子数组。

进阶:

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

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

解题思路:

这里我们一步到位,直接用 O(n log n) 时间复杂度的解法。

​ 我们定义两个指针i、j,i 指向所截取的连续子数组的第一个数,j 指向连续子数组的最后一个数。截取从索引 i 到索引 j 的数组,该数组之和若小于 s,则 j 继续后移,直到大于等于s。记录 j 与 i 差值(返回的目标数)。之后i 后移一位继续刷新新数组。

最坏情况是 i 从0移动到末尾,时间复杂度为O(n),内循环 j 时间复杂度O(log n),总时间复杂度 O(n log n) ,
这道题 Java 提交运行时间:Your runtime beats 99.95 % of java submissions.

Java:
class Solution {
    public int minSubArrayLen(int s, int[] nums) {
        if(nums.length==0)return 0;//空数组则直接返回0
        //返回的目标数 target 定义为最大,sum 起始值为数组第一个数
        int i=0,j=0,numsLen=nums.length,target=Integer.MAX_VALUE,sum=nums[i];
        while (i=numsLen){//如果j等于numsLen,则sum已是从索引i到末位的所有数字之和,后面i无论怎么向后移动均不可能大于s,直接返回target
                    return target==Integer.MAX_VALUE ? 0:target;//如果target值依然为Integer.MAX_VALUE,则意味着i=0,sum为数组所有数之和,则返回0
                }else {
                    sum+=nums[j];//sum向后累加直到大于s
                }
            }
            if(j-i+1
Python3:
class Solution:
    def minSubArrayLen(self, s: int, nums: List[int]) -> int:
        if len(nums)==0: return 0
        i = 0
        j = 0
        nums_len = len(nums)
        target = float("inf")#将target定义为最大
        sum = nums[0]
        while i < nums_len:
            while sum < s:
                j+=1
                if j >= nums_len:
                    return target if target != float("inf") else 0
                else:
                    sum += nums[j]
            target = min(target, j - i + 1)
            sum -= nums[i]
            i+=1
        return target

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/45082.html

相关文章

  • LeetCode 209最小长度的子数组 Minimum Size Subarray Sum

    摘要:如果不存在符合条件的连续子数组,返回。示例输入输出解释子数组是该条件下的长度最小的连续子数组。截取从索引到索引的数组,该数组之和若小于,则继续后移,直到大于等于。记录与差值返回的目标数。之后后移一位继续刷新新数组。 公众号: 爱写bug(ID:icodebugs)作者:爱写bug 给定一个含有 n 个正整数的数组和一个正整数 s ,找出该数组中满足其和 ≥ s 的长度最小的连续子数组...

    jas0n 评论0 收藏0
  • LeetCode 209最小长度的子数组 Minimum Size Subarray Sum

    摘要:如果不存在符合条件的连续子数组,返回。示例输入输出解释子数组是该条件下的长度最小的连续子数组。截取从索引到索引的数组,该数组之和若小于,则继续后移,直到大于等于。记录与差值返回的目标数。之后后移一位继续刷新新数组。 公众号: 爱写bug(ID:icodebugs)作者:爱写bug 给定一个含有 n 个正整数的数组和一个正整数 s ,找出该数组中满足其和 ≥ s 的长度最小的连续子数组...

    JayChen 评论0 收藏0
  • LeetCode 209最小长度的子数组 Minimum Size Subarray Sum

    摘要:如果不存在符合条件的连续子数组,返回。示例输入输出解释子数组是该条件下的长度最小的连续子数组。截取从索引到索引的数组,该数组之和若小于,则继续后移,直到大于等于。记录与差值返回的目标数。之后后移一位继续刷新新数组。 算法是一个程序的灵魂 公众号:爱写bug(ID:icodebugs)作者:爱写bug 给定一个含有 n 个正整数的数组和一个正整数 s ,找出该数组中满足其和 ≥ s 的...

    wow_worktile 评论0 收藏0
  • [LeetCode] 209. Minimum Size Subarray Sum (Easy ve

    Problem 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 isnt one, return 0 instead. Example: Input: s =...

    HelKyle 评论0 收藏0
  • [Leetcode] Minimum Size Subarray Sum 最短子串和

    摘要:双指针复杂度时间空间思路我们用两个指针维护一个窗口,保证这个窗口的内的和是小于目标数的。如果和仍小于目标数,则将窗口右边界右移。另外,如果左边界大于右边界时,说明最短子串的长度已经小于等于,我们就不用再查找了。 Minimum Size Subarray Sum Given an array of n positive integers and a positive integer ...

    wthee 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<