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 ...
随机推荐
- 【Head First Java 读书笔记】(六)认识Java API
第五章 使用Java函数库 ArrayList add(Object elem) remove(int index) remove(Object elem) contains(Object elem) ...
- 去除两张img中间的间隙
这样写 图片之间肯定有间隙 正确写法就是去掉空格 <img src="hlppic.png" /><img src="hlppic.png" ...
- C#分布式存储演练(提供项目下载)
C#简单的演练了一下分布式的存储,学习fastdns的结构,Client向ProcessCenter请求Storage的服务,然后上传文件. 分布式服务就是多个服务器作为客户端互相[配合],要中心化就 ...
- C#帮助类:MD5加密
/// <summary> /// MD5加密 /// </summary> public class Md5 { /// <summary> /// MD5加密 ...
- .NET平台的资源文件管理
可以管理文本.图片等不同类型的资源 管理方式(增删改) 可以直接修改XXX.resx源文件(XML格式,文本直接管理内容,图片需要指定路径,资源名和图片名可以不同) 也可以在VS的可视化界面上进行操作 ...
- 一个简单的tcp代理实现
There are a number of reasons to have a TCP proxy in your tool belt, bothfor forwarding traffic to b ...
- MVC进阶篇(四)——[HttpGet]和[HttpPost]
前言 Get和post,一个获取请求,一个提交请求,在MVC里面用法也很特别,总结一下,我理解的不是特别深刻,希望多多交流. 内容 [HttpGet] 需求: 用户想要通过点击修改按钮来达到修改这部分 ...
- FastDFS分布式⽂文件系统
FastDFS分布式⽂文件系统 1. 什么是FastDFS FastDFS 是⽤用 c 语⾔言编写的⼀一款开源的分布式⽂文件系统.FastDFS 为互联⽹网量量身定制, 充分考虑了了冗余备份.负载均 ...
- SimpleDateFormat线程不安全及解决办法
原文链接:https://blog.csdn.net/csdn_ds/article/details/72984646 以前没有注意到SimpleDateFormat线程不安全的问题,写时间工具类,一 ...
- 深度学习TensorFlow常用函数
tensorflow常用函数 TensorFlow 将图形定义转换成分布式执行的操作, 以充分利用可用的计算资源(如 CPU 或 GPU.一般你不需要显式指定使用 CPU 还是 GPU, Tensor ...