[抄题]:

给定一个由 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. CSS布局相关概要

    一.文档流 运用css布局首先要具备一些概念上的知识,文档流的概念充斥着布局的整个过程.浏览器渲染页面是有先后顺序的,其顺序是至上而下,根据HTML的文档结构进行渲染. 二.div+css 耳熟能详的 ...

  2. StringUtils的工具类isBlank与isEmply

    1. public static boolean isEmpty(String str)   判断某字符串是否为空,为空的标准是 str==null 或 str.length()==0   下面是 S ...

  3. php 数据类型转换与比较

    <?php define("PI", 3.1415926); echo PI."<br>"; //定义一个常量 define("GR ...

  4. 在IIS服务上发布网站

    一.打开控制面板中的“管理工具” 二.打开IIS管理器 三.右键网站,选择“新建网站”

  5. 【c#】设置Socket连接、接收超时(转)

    用到Socket,发现如果连接错误,比如Connect的端口不对,会造成很长时间的延时,程序就僵在那里,效果很不好: 在网上找到很方便的设置办法,分享如下: Socket.SetSocketOptio ...

  6. kafka速度为什么快

    1. kafka 使用了 分区.分布式.leader/followere 的方式.分布式让 kafka 排除了单点故障,分区和分区复制让数据不丢失2. kafka 使用 zero copy 技术 (基 ...

  7. android adb端口被占用解决方法

    1.输入adb devices命令 C:\Users\Nick>adb devices List of devices attached adb server version (31) does ...

  8. linux Xinetd服务简介

    http://www.chuanke.com/course/72351180839190528______.html 1.什么是xinetdextended internet daemonxinetd ...

  9. 黄聪:WordPress 多站点建站教程(三):主站如何调用子站的文章内容、SQL语句如何写?

    1.如果懂得编程的朋友可以SQL语句,然后加上PHP函数等操作就可以通过直接调用网站的数据库信息来实现想要达到的目的. 既然要用到SQL语句首先得对WordPress多站点数据库有一个了解,多站点激活 ...

  10. Python格式化输出%s和%d

    python print格式化输出. 1. 打印字符串 print ("His name is %s"%("Aviad")) 效果: 2.打印整数 print ...