和大于S的最小子数组 · Minimum Size Subarray Sum
[抄题]:
给定一个由 n 个正整数组成的数组和一个正整数 s ,请找出该数组中满足其和 ≥ s 的最小长度子数组。如果无解,则返回 -1。
给定数组 [2,3,1,2,4,3]
和 s = 7
, 子数组 [4,3]
是该条件下的最小长度子数组。
[暴力解法]:
时间分析:
空间分析:
[思维问题]:
- 和 ≥ s 的最小长度子数组,和《s时j++,达到后更新j-i。再扫更大,所以此处打止。(j不用回去,否则会变成原来的i)
- 比小时,初始化为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的更多相关文章
- 领扣-209 长度最小的子数组 Minimum Size Subarray Sum MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
- LeetCode 209:最小长度的子数组 Minimum Size Subarray Sum
公众号: 爱写bug(ID:icodebugs) 作者:爱写bug 给定一个含有 n 个正整数的数组和一个正整数 s ,找出该数组中满足其和 ≥ s 的长度最小的连续子数组.如果不存在符合条件的连续子 ...
- [LintCode] Minimum Size Subarray Sum 最小子数组和的大小
Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...
- leetcode面试准备:Minimum Size Subarray Sum
leetcode面试准备:Minimum Size Subarray Sum 1 题目 Given an array of n positive integers and a positive int ...
- [LeetCode] Minimum Size Subarray Sum 解题思路
Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...
- 【刷题-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 ...
- [LeetCode] Minimum Size Subarray Sum 最短子数组之和
Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...
- [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 ...
- Minimum Size Subarray Sum 最短子数组之和
题意 Given an array of n positive integers and a positive integer s, find the minimal length of a suba ...
随机推荐
- RF/GBDT/XGBoost/LightGBM简单总结(完结)
这四种都是非常流行的集成学习(Ensemble Learning)方式,在本文简单总结一下它们的原理和使用方法. Random Forest(随机森林): 随机森林属于Bagging,也就是有放回抽样 ...
- ZetCode PyQt4 tutorial custom widget
#!/usr/bin/python # -*- coding: utf-8 -*- """ ZetCode PyQt4 tutorial In this example, ...
- gradle 代理设置
可以对gradle全局设置变量,也可以针对单个项目进行配置. 只需要在.gradle目录下创建一个gradle.properties文件,文件中按照如下格式添加代理配置 systemProp.http ...
- 比较两个array或者object是否深度相等
function objectEquals(object1: Object, object2: Object): boolean { for (let propName in object1) { i ...
- 20155313 2016-2017-2 《Java程序设计》第八周学习总结
20155313 2016-2017-2 <Java程序设计>第八周学习总结 教材内容学习 十四章 NIO与NIO2 1.认识NIO NIO使用频道(Channel)来衔接数据节点,在处理 ...
- 20155236 2016-2017-2 《Java程序设计》第六周学习总结
20155236 2016-2017-2 <Java程序设计>第六周学习总结 教材学习内容总结 InputStream与OutputStream 从应用程序角度来看,如果要将数据从来源取出 ...
- sql server常用日期格式化
/*8 24 108 - hh:mm:ss */ Select CONVERT(varchar(), GETDATE(), )-- :: Select CONVERT(varchar(), GETDA ...
- 使用JS 加入收藏,设为首页.
<script type="text/javascript" language="javascript"> function AddFavorite ...
- ThinkJava-压缩
尽管存在许多种压缩算恙,但是Zip和GZIP可能是最常用的.因此我们可以很容易地使用多 种可读写这些格式的工具来操纵我们的压缩数据. 1 用GZIP进行简单压缩 GZIP接口非常简单, 因此如果我 ...
- CentOS开机自启动
CentOS 配置的开机自启动. vim /etc/rc.local #!/bin/sh # # This script will be executed *after* all the other ...