【LeetCode】862. Shortest Subarray with Sum at Least K 解题报告(C++)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/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 is no non-empty subarray with sum at least K, return -1.
Example 1:
Input: A = [1], K = 1
Output: 1
Example 2:
Input: A = [1,2], K = 4
Output: -1
Example 3:
Input: A = [2,-1,2], K = 3
Output: 3
Note:
- 1 <= A.length <= 50000
- -10 ^ 5 <= A[i] <= 10 ^ 5
- 1 <= K <= 10 ^ 9
题目大意
求最短的子数组长度,使得这个子数组的和最少为K,如果不存在这样的子数组,返回-1.
解题方法
队列
我尝试了O(N^2)的解法,果然超时了。也对,题目给出的数组长度是50000,基本上只有O(N)或者O(NlogN)的时间复杂度才行了。
这个题的做法要感谢lee215和演员的自我修养。下面的内容来自演员的自我修养。
分析:
- 显然,我们会想到使用dp[i]记录sum(A[:i]),那么这道题就变成了,给定一个数组dp,找到一组i,j,使得dp[j]-dp[i]>=K,且j-i尽量小!
- 数据长度达到50000,显然不能使用O(n^2)复杂度的方法,我们得想办法让i,j只走一遍
- 用一个简单的示例来分析,设 A = [4,-1,2,3],,K = 5,那么dp = [0,4,3,5,8],我们从dp数组的第2个数开始分析,(假设来了个-1,那么因为-1比0小,后面任意一个数val如若满足val-0>K,那么val+1也一定大于K,且-1所在的位置i显然能获得更优解,所以0这个位置就失去了意义),现在考虑示例,来了个4,我们发现4-0小于5,我们怎么对4进行处理呢,因为考虑到之后或许会出现一个足够大的数,比如9,那么4相对于0是更优的,但也有可能只来一个8,那么4就没作用了,所以先暂且保留观察。等到来了一个5以上的数,我们依次对保留的数(目前是0,4)进行判断得最优解。
- 接下来来了个3,那么根据上面提到的论点,4将会被舍弃,但3比0要大,故此时0,3保留。
- 然后来了个5,5-0>=5,故找到一组i,j,记录下来,然后判断 5-3>=5 ?如若确实大于,即再次找到一组i,j,若小于,则5保留(考虑到之后或许来了个10),依次类推
思路:
- 建立一个队列记录保留数字,初始为0
- 依次对dp中的数进行分析,如果dp[i] - dp[Q[0]] >= K,则记录一次i,j
- 如果dp[i] < dp[Q[-1]],则舍弃Q[-1]
C++代码如下:
class Solution {
public:
int shortestSubarray(vector<int>& A, int K) {
const int N = A.size();
vector<int> preSum(N + 1, 0);
for (int i = 1; i < N + 1; i++) {
preSum[i] = preSum[i - 1] + A[i - 1];
}
int res = INT_MAX;
deque<int> q;
q.push_back(0);
for (int i = 1; i < N + 1; i++) {
int a = preSum[i];
while (!q.empty() && a - preSum[q.front()] >= K) {
res = min(res, i - q.front());
q.pop_front();
}
while (!q.empty() && a < preSum[q.back()]) {
q.pop_back();
}
q.push_back(i);
}
return res == INT_MAX ? -1 : res;
}
};
参考资料:
https://leetcode.com/problems/shortest-subarray-with-sum-at-least-k/discuss/143726/C%2B%2BJavaPython-O(N)-Using-Deque
https://buptwc.com/2018/07/02/Leetcode-862-Shortest-Subarray-with-Sum-at-Least-K/
日期
2018 年 12 月 20 日 —— 感冒害的我睡不着
【LeetCode】862. Shortest Subarray with Sum at Least K 解题报告(C++)的更多相关文章
- [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 ...
- 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 ...
- leetcode 862 shorest subarray with sum at least K
https://leetcode.com/problems/shortest-subarray-with-sum-at-least-k/ 首先回顾一下求max子数组的值的方法是:记录一个前缀min值, ...
- [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】1099. Two Sum Less Than K 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力求解 日期 题目地址:https://leetco ...
- 【LeetCode】599. Minimum Index Sum of Two Lists 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:找到公共元素再求索引和 方法二:索引求和,使 ...
- 【LeetCode】375. Guess Number Higher or Lower II 解题报告(Python)
[LeetCode]375. Guess Number Higher or Lower II 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...
- 【LeetCode】430. Flatten a Multilevel Doubly Linked List 解题报告(Python)
[LeetCode]430. Flatten a Multilevel Doubly Linked List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: ...
随机推荐
- CentOS6.9 内核升级详解
内核进行的是应用软件和计算机硬件的交互工作在计算机科学中,内核(英语:kernel)又称核心,是一个计算机程序,用来管理软件发出的数据I/O(输入与输出)要求,将这些要求转译为数据处理的指令,交由中央 ...
- 56-Remove Linked List Elements
Remove Linked List Elements My Submissions QuestionEditorial Solution Total Accepted: 61924 Total Su ...
- CSS上下左右居中对齐
上下左右居中对齐 display: inline/inline-block 将父元素(容器)设定 text-align: center: 即可左右置中. display: block 将元素本身的 ...
- day 03Linux修改命令提示符
day 03Linux修改命令提示符 昨日回顾 1.选择客户机操作系统: Microsoft Windows # 一次只能安装一台电脑 Linux(推荐) VMware ESX # 服务器版本VNwa ...
- Leetcode中的SQL题目练习(二)
175. Combine Two Tables https://leetcode.com/problems/combine-two-tables/description/ Description Pe ...
- HTTP请求 Java API
1.导入依赖 <dependency> <groupId>commons-httpclient</groupId> <artifactId>common ...
- 使用WtmPlus低代码平台提高生产力
低代码平台的概念很火爆,产品也是鱼龙混杂. 对于开发人员来说,在使用绝大部分低代码平台的时候都会遇到一个致命的问题:我在上面做的项目无法得到源码,完全黑盒.一旦我的需求平台满足不了,那就是无解. ...
- 转 关于HttpClient,HttpURLConnection,OkHttp的用法
转自:https://www.cnblogs.com/zp-uestc/p/10371012.html 1 HttpClient入门实例 1.1发送get请求 1 2 3 4 5 6 7 8 9 10 ...
- oracle 拆分字符串
WITH t AS (SELECT '1-2-3-4' a FROM dual)SELECT Regexp_Substr(a, '[^-]+', 1, LEVEL) i FROM tCONNECT B ...
- LINUX 安装增强 前置安装文件
yum install kernel yum install kernel-devel yum install gcc yum install make