公众号: 爱写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. 数据库——SQL-SERVER CREATE-TABLES

    给出数据库实验所需要的“CREATE-TABLES.SQL”文件 use master go if exists (select * from dbo.sysdatabases where name ...

  2. idea使用lombok不生效的解决办法

    file-->setting-->plugins点击下方的 browse repositories. 搜索lombok plugin. 安装后,重启. file-->setting- ...

  3. vs未能正确加载CSharpPackage包,未能正确加载“Microsoft.VisualStudio.Editor.Implementation.EditorPackage”包

    VS2017打开项目时提示未能正确加载CSharpPackage包, 可以使用 devenv命令工具来解决,操作如下 打开vs2017开发人员命令提示符(请使用管理员身份运行),如图 敲入  deve ...

  4. C#反射调用类的私有方法

    void Main() { var type = typeof(StockClass); type.Dump(); var fields=type.GetFields(BindingFlags.Ins ...

  5. c#微信公众号开发一----基本设置,服务器配置token验证,获取timestamp/nonce/signature

    一.c#微信公众号开发----基本设置 参考微信官方文档 https://developers.weixin.qq.com/doc/offiaccount/Basic_Information/Acce ...

  6. PlayJava Day003

    今日所学: /* 2019.08.19开始学习,此为补档. */ ①char:只能有一个字段.字符:' ' ②二进制:0000 0000 最后一位为0就不算,为1代表20. 如25为:0001 100 ...

  7. .NET MVC5简介(二)

    MVCApplication---Application_Statr--RegisterRoutes--给RouteCollection添加规则,请求进到网站---X----请求地址被路由按照顺序匹配 ...

  8. HTML学习 day03

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

  9. Swift相比OC语言有哪些优点

    Swift相比OC语言有哪些优点 1.自动做类型推断 2.可以保证类型使用安全 Swif类型说明符 --Swift增加了Tuple表示元组类型 --Swift增加了Optional表示可选类型 常量一 ...

  10. zabbix4.0搭建1

    server端:负责接受到客户端发送过来的数据,并且保存到自己的数据库当中 端口:10051 agent端:负责每隔一定时间进行客户端的数据采集,并且发送给server端 端口:10050 proxy ...