和大于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 ...
随机推荐
- iOS语法糖 简单却不那么简单
转载作者 香蕉大大 (Github) 开发过程中我特别喜欢用语法糖,原因很简单,懒得看到一堆长长的代码,但是语法糖我今天无意中看到更有意思的玩法.这里暂时吧把今天新学到的知识点整理一下希望大家喜欢,如 ...
- axios请求requestBody和formData
前言 在vue的后台管理开发中,应需求,需要对信息做一个校验,需要将参数传递两份过去,一份防止在body中,一份防止在formdata中,axios请求会默认将参数放在formdata中进行发送. 对 ...
- iPhone设备及屏幕适配
// // Common.h // 微信 // // #ifndef Common_h #define Common_h // iPhone设备及屏幕适配 //4的设备 #define KDevice ...
- android logger的使用
1. 进入GitHub页面 https://github.com/orhanobut/logger 2. 在gradle里增加 compile 'com.orhanobut:logger:1.15' ...
- POI2014题解
POI2014题解 [BZOJ3521][Poi2014]Salad Bar 把p当作\(1\),把j当作\(-1\),然后做一遍前缀和. 一个合法区间\([l,r]\)要满足条件就需要满足所有前缀和 ...
- HDU 3973 AC's String 字符串哈希
HDU 3973 通过哈希函数将一个字符串转化为一个整数,通过特定的方式可以使得这个哈希值几乎没有冲突(这就是关键之处,几乎没有视为没有= =!, 其实也可以考虑实现哈希冲突时的处理,只是这道题没必要 ...
- test20181025 Color
题意 分析 自己的想法 可以莫队+平衡树. 对每个颜色维护一颗平衡树,然后移动莫队端点的时候在平衡树中查询. 区间加操作容易实现. 单点修改转化为平衡树的插入删除. 感谢Z前辈的指导. 时间复杂度\( ...
- Django中MySQL读写分离技术
最近需要用到Django的MySQL读写分离技术,查了一些资料,把方法整理了下来. 在Django里实现对MySQL的读写分离,实际上就是将不同的读写请求按一定的规则路由到不同的数据库上(可以是不同类 ...
- 1.Appium环境搭建
1.安装node.js (1)去node官网下载,根据操作系统的不同选择不同对应的版本(https://nodejs.org/en/download/) (2)下载对应的版本后进行安装,一直下一步直至 ...
- wiremock 模拟服务接口提供前端使用
前后端分离同步开发时,如果前端需要等后端把接口都开发完了再去动工的话,项目周期会拉长. 以前开发时,一般前期是先把接口文档写的差不多了,要么是让前端自己构造模拟数据,要么是后端在开个控制器专门提供模拟 ...