[抄题]:

给定一个由 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. Unable to connect to any of the specified MySQL hosts.

    c#连接Mysql数据建立连接时提示:Unable to connect to any of the specified MySQL hosts. 出现此错误的原因是Server(数据库服务器IP地址 ...

  2. CMake与Make

    大家都知道,写程序大体步骤为: 1.用编辑器编写源代码,如.c文件. 2.用编译器编译代码生成目标文件,如.o. 3.用链接器连接目标代码生成可执行文件,如.exe. 但如果源文件太多,一个一个编译时 ...

  3. CF1082G:G. Petya and Graph(裸的最大闭合权图)

    Petya has a simple graph (that is, a graph without loops or multiple edges) consisting of n n vertic ...

  4. 20155223 2016-2017-2 《Java程序设计》第8周学习总结

    20155223 2016-2017-2 <Java程序设计>第8周学习总结 教材学习内容总结 第14章 Channel是Java NIO用来衔接数据节点的功能,可定义缓冲区容量.标记内容 ...

  5. Python面试题(十二)

    1.利用Python执行shell脚本取得返回结果 import subprocess result = subprocess.getoutput('dir') 答案 2.用Python将" ...

  6. OLEDB操作Excel

    使用OLEDB操作Excel 的方法 OleDbConnection conn = null;            try            {              //fileName ...

  7. UVA之1121 - Subsequence

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/SunnyYoona/article/details/25840365 [题目] A sequence ...

  8. windows内存debug技巧

    A) c++ memory/heap corrupt debug 技巧 1. catch first exception2. data breakpointVC tell us some addres ...

  9. linux nginx不区别大小写处理方法

    # 把所有的目录及文件名全改成小写,注意之后新增目录及文件只使用小写命名 递归转换目录或文件名方法:python把指定目录下的递归所有目录和文件名转换成小写或大写(http://www.cnblogs ...

  10. php 面试一般都遇到什么问题

    大型互联网公司会从几个方面来考核:第一:专业上,专业分为五个方向,操作系统,网络,算法,语言,数据库,一般情况下,会比较在乎Linux系统的日常使用,包括shell脚本,比较深入的话,会问kernel ...