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的更多相关文章

  1. [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 ...

  2. 【LeetCode】862. Shortest Subarray with Sum at Least K 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 队列 日期 题目地址:https://leetcod ...

  3. 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 ...

  4. [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 ...

  5. 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 ...

  6. [LeetCode] Minimum Size Subarray Sum 解题思路

    Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...

  7. 【LeetCode】560. Subarray Sum Equals K 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  8. [array] leetcode - 53. Maximum Subarray - Easy

    leetcode - 53. Maximum Subarray - Easy descrition Find the contiguous subarray within an array (cont ...

  9. [LeetCode] Maximum Average Subarray I 子数组的最大平均值

    Given an array consisting of n integers, find the contiguous subarray of given length k that has the ...

随机推荐

  1. MongoDB整理笔记のCRUD

    添加 下面我们来建立一个test 的集合并写入一些数据.建立两个对象j 和t , 并保存到集合中去.在例子里 “>” 来表示是 shell 输入提示符    > j = { name : ...

  2. 利用Response.Buffer做类似异步效果

    在page_load加入以下代码 Response.Buffer = false;  //这句话非常重要 for (int i = 0; i < 10; i++) { Thread.Sleep( ...

  3. MongoDB单表导出与导入

    mongoexport -h -u dbAdmin -p L-$LpGQ=FJvSf*****([l --authenticationDatabase=project_core_db -d proje ...

  4. 在构造函数和析构函数中调用虚函数------新标准c++程序设计

    在构造函数和析构函数中调用虚函数不是多态,因为编译时即可确定调用的是哪个函数.如果本类有该函数,调用的就是本类的函数:如果本类没有,调用的就是直接基类的函数:如果基类没有,调用的就是间接基类的函数,以 ...

  5. django中博客后台将图片上传作为用户头像

    添加上传目录 # 如果不添加上传目录,仍然可以上传成功,默认为project目录,如果models.py定义了upload_to="目录名称",则会上传到"project ...

  6. git配置本地环境(phpstudy/tortoisegit/git等)

    1.下载安装phpstudy 2.下载安装git 下载地址:https://git-scm.com/downloads 3.下载安装tortoisegit,电脑64位就下载这个,如图: 4.下载安装“ ...

  7. jmeter函数助手(_random、_time)

    jmeter函数助手 __random __time yyyyMMddHHmmdd  时间格式(年月日时分秒) 1.打开函数助手对话框,选项->函数助手对话框 2.生成函数字符串 (1)选择功能 ...

  8. Jmeter_录制HTTPS

    [环境] Jmeter版本:Jmeter3.2: JDK版本:JDK1.8 [配置] [1]添加“线程组.Http信息头管理器.httpCookie管理器.HTTP代理服务器”: [2]设置浏览器的“ ...

  9. 【bzoj4804】欧拉心算 莫比乌斯反演+莫比乌斯函数性质+线性筛

    Description 给出一个数字N 求\(\sum_{i=1}^{n}\sum_{j=1}^{n}\varphi(gcd(i,j))\) Input 第一行为一个正整数T,表示数据组数. 接下来T ...

  10. luoguP4396 [AHOI2013]作业

    https://www.luogu.org/problemnew/show/P4396 简单的莫队+树状数组,但博主被卡常了,不保证代码在任何时候都能AC #include <bits/stdc ...