[抄题]:

给定一个由 n 个正整数组成的数组和一个正整数 s ,请找出该数组中满足其和 ≥ s 的最小长度子数组。如果无解,则返回 -1。

给定数组 [2,3,1,2,4,3] 和 s = 7, 子数组 [4,3] 是该条件下的最小长度子数组。

[暴力解法]:

时间分析:

空间分析:

[思维问题]:

  1. 和 ≥ s 的最小长度子数组,和《s时j++,达到后更新j-i。再扫更大,所以此处打止。(j不用回去,否则会变成原来的i)
  2. 比小时,初始化为Integer.MAX_VALUE,忘了。

[一句话思路]:

长度不确定的窗口:boy追逐girl, girl在boy的循环中发生先量变、再质变

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

长度不确定的窗口:boy追逐girl, girl在boy的循环中发生先量变、再质变

[复杂度]:Time complexity: O(2n) Space complexity: O(n)

boy 一共走n, girl一共也走只n,而不是每次都跟着 。同时并行而不是嵌套,故为2n

[英文数据结构或算法,为什么不用别的数据结构或算法]:

[关键模板化代码]:

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

718. Maximum Length of Repeated Subarray dp这么麻烦的吗

[代码风格] :

public class Solution {
/**
* @param nums: an array of integers
* @param s: An integer
* @return: an integer representing the minimum size of subarray
*/
public int minSubArrayLen(int s, int[] nums) {
//corner case
if (nums == null || s <= 0) {
return -1;
}
//i,j in same dir
int i = 0, j = 0;
int sum = 0;
int ans = Integer.MAX_VALUE;
for (i = 0; i < nums.length; i++) {
//accumulate
while (j < nums.length && sum < s) {
sum += nums[j];
j++;
}
//change
if (sum >= s) {
ans = Math.min(ans, j - i);
}
//boy should go back
sum -= nums[i];
}
//if no result
if (ans == Integer.MAX_VALUE) {
return -1;
} return ans;
}
}

和大于S的最小子数组 · Minimum Size Subarray Sum的更多相关文章

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

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

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

    公众号: 爱写bug(ID:icodebugs) 作者:爱写bug 给定一个含有 n 个正整数的数组和一个正整数 s ,找出该数组中满足其和 ≥ s 的长度最小的连续子数组.如果不存在符合条件的连续子 ...

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

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

  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. 【刷题-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 ...

  7. [LeetCode] Minimum Size Subarray Sum 最短子数组之和

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

  8. [LeetCode] 209. Minimum Size Subarray Sum 最短子数组之和

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

  9. Minimum Size Subarray Sum 最短子数组之和

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

随机推荐

  1. 使用c++实现一个FTP客户端(一)

    之前使用c++实现了一个FTP客户端,在这里做一些记录. 一.需要注意的几点 ①FTP是一种文件传输协议,基于TCP,所以客户端与服务器建立的连接是可靠.安全的,并且要经过三次握手的过程. ②FTP传 ...

  2. 工作中比较重要的经验分享-2016-bypkm

    工作中总有一些经验能让人记忆深刻,能让人终生受用,相比技术而言,经验是宝贵的.无价的.在我的博客中,主要是技术类的博文,那些东西是相对死板的,价值也相对低廉.今天就记录一下我在工作中一次比较重要的经验 ...

  3. python3高阶函数:map(),reduce(),filter()的区别

    转载请注明出处:https://www.cnblogs.com/shapeL/p/9057152.html 1.map():遍历序列,对序列中每个元素进行操作,最终获取新的序列 print(list( ...

  4. C程序设计-----第2次作业

    作业要求一 (15分) 完成下列编程题目,每次上完课都会增加2-3道题目,并将编程过程记录在博客里,一次PTA作业任选一道题目给出设计思路.流程图.源代码和错误记录,其他题目可只给出设计思路.源代码和 ...

  5. sourcegraph 方便的代码查看工具

    sourcegraph 是一个方便的代码查看插件,有chrome 的插件,具体安装可以在chrome 应用商店,同时 官方提供了基于docker 运行的方式(适合本地使用) 下载镜像 docker p ...

  6. vs2005 sp1 补丁的安装问题

    最近做windows mobile 6.0的手机软件开发,听说用vs2005 开发的话最少得装vs2005 sp1,于是去官网上下了VS80sp1-KB926604-X86-CHS.exe 补丁 .运 ...

  7. 黄聪:win7 64位系统PS、AI、PSD缩略图预览补丁

    MysticThumbs支持Windows 7 / Vista / XP,32位和64位.除了预览PSD以外,还支持DDS.SGI缩略图显示. Mystic Thumbs是一款用来支持win7 64位 ...

  8. RDLC报表系列一

    1.报表项目搭建: 配置好后,单击Web服务URL:http://lg-20151517ryre/ReportServer 如果电脑系统打开的时候没有设置密码的话,此时打开有可能会出现需要登录名和密码 ...

  9. Java利用ScriptEngineManager对计算公式的支持

    1.ScriptEngineManager是JDK6提出的相关方法,这方式的主要目的就是用来对脚本语言的处理.这里只是简单介绍一下对我们常用的数学公式的应用. 2.ScriptEngineManage ...

  10. 第 0 课 Golang环境搭建

    1  下载 https://studygolang.com/dl  2   安装完成后,查看环境变量 GOBIN 存放可执行文件的目录的绝对路径. GOPATH 工作区目录的绝对路径.存放远源码的路径 ...