leetcode 862 shorest subarray with sum at least K
https://leetcode.com/problems/shortest-subarray-with-sum-at-least-k/
首先回顾一下求max子数组的值的方法是:记录一个前缀min值,然后扫一遍sum数组。
1、首先这里不需要最大,因为刚好够k就好了
2、这里需要距离最短。就是数组的长度最短。
这里的思路也一样,不过保存很多个min值,就是用一个队列,保存前缀的min值,不需要最min,只不过有更小的就更好。
也就是如果sum数组的值是:
..... 60, 40.....Y.....
那么在原本的队列中,60可以pop出来了,因为他被40代替了,因为Y减去40的值肯定比60大,更有可能大于k,而且这样距离也更短。
class Solution {
public:
int shortestSubarray(vector<int> A, int k) {
int len = (int)A.size();
deque<int> que;
vector<int> sum(len + 1, 0);
for (int i = 0; i < len; ++i) {
sum[i + 1] = sum[i] + A[i];
}
int ans = len + 1;
que.push_back(0);
for (int i = 1; i <= len; ++i) {
while (que.size() > 0 && sum[i] - sum[que.front()] >= k) {
ans = min(ans, i - que.front());
que.pop_front();
}
while (que.size() > 0 && sum[i] <= que.back()) {
que.pop_back();
}
que.push_back(i);
}
return ans == len + 1 ? -1 : ans;
}
} t;
leetcode 862 shorest subarray with sum at least K的更多相关文章
- [LeetCode] 862. Shortest Subarray with Sum at Least K 和至少为K的最短子数组
Return the length of the shortest, non-empty, contiguous subarray of A with sum at least K. If there ...
- 【LeetCode】862. Shortest Subarray with Sum at Least K 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 队列 日期 题目地址:https://leetcod ...
- 862. Shortest Subarray with Sum at Least K
Return the length of the shortest, non-empty, contiguous subarray of A with sum at least K. If there ...
- [Swift]LeetCode862. 和至少为 K 的最短子数组 | Shortest Subarray with Sum at Least K
Return the length of the shortest, non-empty, contiguous subarray of A with sum at least K. If there ...
- LeetCode862. Shortest Subarray with Sum at Least K
Return the length of the shortest, non-empty, contiguous subarray of A with sum at least K. If there ...
- [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】560. Subarray Sum Equals K 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- [array] leetcode - 53. Maximum Subarray - Easy
leetcode - 53. Maximum Subarray - Easy descrition Find the contiguous subarray within an array (cont ...
- [LeetCode] Maximum Average Subarray I 子数组的最大平均值
Given an array consisting of n integers, find the contiguous subarray of given length k that has the ...
随机推荐
- Oracle 触发器 删除操作时再查询本表数据 功能不正确
背影如下: 表名,WFGTEST create table WFGTEST ( NAME1 ) not null, NAME2 ), CAPACITY ,) ) 表结构如下: NAME1 NAME2 ...
- vs2015编译ffmpeg 出现错误rtmp.lib(rtmp.obj) : error LNK2001: 无法解析的外部符号 ___iob_func
vs2015编译ffmpeg(版本3.0.2)引用外部库文件librtmp出现以下错误: rtmp.lib(rtmp.obj) : error LNK2001: 无法解析的外部符号 __imp__st ...
- Struts2学习第2天--Struts2的Servlet的API的访问 Struts2的结果页面的配置 Struts2的数据的封装(包括复杂类型)
启动后访问jsp 输入姓名密码: 提交后跳转打action 打印: 修改类: 配置同上 结果同上. 实现这俩接口 就得到了 以上代码附上: struts.xml: <?xml version=& ...
- slowhttptest安装及使用
slowhttptest简介: Slowhttptest是依赖HTTP协议的慢速攻击DoS攻击工具,设计的基本原理是服务器在请求完全接收后才会进行处理,如果客户端的发送速度缓慢或者发送不完整,服务端为 ...
- .Generator与其他异步处理方案
1.Generator与其他异步处理方案 以前,异步编程的方法,大概有下面四种. 1.1 回调函数 JavaScript 语言对异步编程的实现,就是回调函数.所谓回调函数,就是把任务的第二段单独写在一 ...
- struts2配置文件中的method={1}详解
struts.xml中的配置: <!-- 配置用户模块的action --> <action name="user_*" class="userActi ...
- Python实现——一元线性回归(最小二乘法)
2019/3/24 线性回归--最小二乘法公式法 暂时用python成功做出来了图像,但是其中涉及到的公式还是更多的来自于网络,尤其是最小二乘法公式中的两个系数的求解,不过目前看了下书高数也会马上提及 ...
- Java过滤器详细文档,简介,实例,应用
简介 Filter也称之为过滤器,它是Servlet技术中最激动人心的技术,WEB开发人员通过Filter技术,对web服务器管理的所有web资源:例如Jsp, Servlet, 静态图片文件或静态 ...
- python学习之路---day008
文件操作一:文件操作01):文件读取:(r 只读) 001):我们先在当前文件夹内创建txt文件取名为123,在其中添加几句话内容. f 称之为文件句柄,控制着 123 这个文本文档 f=open(& ...
- paraview添加vector
https://youtu.be/cygVdhn-kG0 (须自行FQ)