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 ...
随机推荐
- C++中的Trivial 、POD、non-POD和Standard Layout概念
POD types non-POD types Standard Layout types A Formal Definition Informally, a standard layout clas ...
- 申请参加腾讯云自媒体分享计划,送代金券+CN域名
文章介绍 看到腾讯支持博客了,就尝试下,看看效果如何. 文章属于转载,文末有文章来源,转载注明出处. 活动规则 活动资格:面向腾讯云官网已注册且完成实名认证用户,除协作者之外,其余用户均可参与; 如何 ...
- C#设计模式系列:适配器模式(Adapter Pattern)
一.引言 在软件系统中,由于应用环境的变化,常常需要将“一些现存的对象”放在新的环境中应用.但是新环境要求的接口是这些现存对象所不满足的.如何应对这种“迁移的变化”?如何既能利用现有对象的良好实现,同 ...
- 浅谈PHP的Public、Protected、Private三种方法的区别
public:权限是最大的,可以内部调用,实例调用等.protected: 受保护类型,用于本类和继承类调用.private: 私有类型,只有在本类中使用. <?php error_report ...
- Centos文章列表
1.Linux 中将用户添加到组的指令:https://cnzhx.net/blog/linux-add-user-to-group/ 2.CentOS7为firewalld添加开放端口及相关操作:h ...
- CentOS7.4关闭防火墙
//临时关闭 systemctl stop firewalld //禁止开机启动 systemctl disable firewalld Removed symlink /etc/systemd/sy ...
- 图像金字塔、高斯金字塔、差分金字塔(DOG金字塔)、尺度空间、DoG (Difference of Gaussian)角点检测
[图像金字塔] 图像金字塔是一种以多分辨率来解释图像的结构,通过对原始图像进行多尺度像素采样的方式,生成N个不同分辨率的图像.把具有最高级别分辨率的图像放在底部,以金字塔形状排列,往上是一系列像素(尺 ...
- jquery查找子元素和兄弟元素
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- office 2013 破解工具 及 软件下载
win7/win8/win10 office2013 破解工具 下载地址: https://pan.baidu.com/s/1sZeJOCWq1fZ3KIOWvmrAQQ office2013 ...
- tornado 04 模板
一.模板 #模板就是一个HTML文件,只是其中添加了模板语法,需要服务器的渲染才能正常显示数据 #通过render可以返回一个html网页,不过那都是固定的页面,固定的数据,如果数据是不确定的,是会不 ...