公众号: 爱写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){
while (sum<s){
if(++j>=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<target) target=j-i+1;//刷新target的值
sum-=nums[i++];//sum移去i的值得到新数组之和,i进一位
}
return target;
}
}

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

LeetCode 209:最小长度的子数组 Minimum Size Subarray Sum的更多相关文章

  1. 领扣-209 长度最小的子数组 Minimum Size Subarray Sum MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  2. 和大于S的最小子数组 · Minimum Size Subarray Sum

    [抄题]: 给定一个由 n 个正整数组成的数组和一个正整数 s ,请找出该数组中满足其和 ≥ s 的最小长度子数组.如果无解,则返回 -1. 给定数组 [2,3,1,2,4,3] 和 s = 7, 子 ...

  3. 【刷题-LeetCode】209. Minimum Size Subarray Sum

    Minimum Size Subarray Sum Given an array of n positive integers and a positive integer s, find the m ...

  4. leetcode面试准备:Minimum Size Subarray Sum

    leetcode面试准备:Minimum Size Subarray Sum 1 题目 Given an array of n positive integers and a positive int ...

  5. [LeetCode] Minimum Size Subarray Sum 解题思路

    Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...

  6. [LintCode] Minimum Size Subarray Sum 最小子数组和的大小

    Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...

  7. LeetCode 209. 长度最小的子数组(Minimum Size Subarray Sum)

    题目描述 给定一个含有 n 个正整数的数组和一个正整数 s ,找出该数组中满足其和 ≥ s 的长度最小的连续子数组.如果不存在符合条件的连续子数组,返回 0. 示例: 输入: s = 7, nums ...

  8. **209. Minimum Size Subarray Sum 长度最小的子数组

    1. 题目描述 给定一个含有 n 个正整数的数组和一个正整数 s ,找出该数组中满足其和 ≥ s 的长度最小的连续子数组.如果不存在符合条件的连续子数组,返回 0. 示例: 输入: s = 7, nu ...

  9. [Swift]LeetCode209. 长度最小的子数组 | Minimum Size Subarray Sum

    Given an array of n positive integers and a positive integer s, find the minimal length of a contigu ...

随机推荐

  1. 在Unity中使用自定义宏

    最近写AVG工具时有这样的功能需求,AVG的角色可以支持动态的Spine动画,当没有Spine动画时采用默认的立绘图片替代. 这时在脚本中就可以采用自定义的宏来实现: 例如: #if VNSpine ...

  2. 记一次收集APP native崩溃信息

    最近在学习 极客时间Android开发高手课 老师推荐了Breakpad开源库来采集native 的crash1.为什么要使用Google Breakpad? 我们在开发过程中,Android JNI ...

  3. Java 类加载机制(阿里)-何时初始化类

    (1)阿里的面试官问了两个问题,可以不可以自己写个String类 答案:不可以,因为 根据类加载的双亲委派机制,会去加载父类,父类发现冲突了String就不再加载了; (2)能否在加载类的时候,对类的 ...

  4. c# winform 窗体失去焦点关闭(钩子实现)

    先来一个辅助类 using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Inte ...

  5. WPF线段式布局的一种实现

    线段式布局 有时候需要实现下面类型的布局方案,不知道有没有约定俗成的称呼,我个人强名为线段式布局.因为元素恰好放置在线段的端点上. 实现 WPF所有布局控件都直接或间接的继承自System.Windo ...

  6. Abp vNext框架 实例程序BookStore-笔记

    参考 Abp vNext框架 应用程序开发教程 创建项目和书籍列表页面 http://www.vnfan.com/helinbin/d/3579c6e90e1d23ab.html 官方源码 https ...

  7. 用一个bat文件调用另外两个bat文件,当1.bat执行完后再执行2.bat

    用一个bat文件调用另外两个bat文件,当1.bat执行完后再执行2.bat 摘自:https://zhidao.baidu.com/question/492732911.html @echo off ...

  8. 尉蓝色的P2P金融众筹平台手机模板

    蓝色的p2p金融投资众筹网手机模板html整站下载.实用的众筹app手机模板下载.主要页面有:众筹项目.发布.个人中心.登录.注册.优惠券.回报.项目详情.我要支持.帮助中心等总共37个手机页面. 模 ...

  9. JS基础语法---break关键字

    break关键字: 如果在循环中使用,遇到了break,则立刻跳出当前所在的循环       for (var i = 0; i < 10; i++) {         while (true ...

  10. HTML学习 day03

    表单 表单   表单:表单域:包含了处理表单数据所用的程序的URL以及数据提交到服务器的方法.     表单控件:(对象.元素):包含了文本框.密码框.隐藏.多行文本框(文本域).复选框.单选框.下拉 ...