作者: 负雪明烛
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. 1 <= A.length <= 50000
  2. -10 ^ 5 <= A[i] <= 10 ^ 5
  3. 1 <= K <= 10 ^ 9

题目大意

求最短的子数组长度,使得这个子数组的和最少为K,如果不存在这样的子数组,返回-1.

解题方法

队列

我尝试了O(N^2)的解法,果然超时了。也对,题目给出的数组长度是50000,基本上只有O(N)或者O(NlogN)的时间复杂度才行了。

这个题的做法要感谢lee215和演员的自我修养。下面的内容来自演员的自我修养

分析:

  1. 显然,我们会想到使用dp[i]记录sum(A[:i]),那么这道题就变成了,给定一个数组dp,找到一组i,j,使得dp[j]-dp[i]>=K,且j-i尽量小!
  2. 数据长度达到50000,显然不能使用O(n^2)复杂度的方法,我们得想办法让i,j只走一遍
  3. 用一个简单的示例来分析,设 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)进行判断得最优解。
  4. 接下来来了个3,那么根据上面提到的论点,4将会被舍弃,但3比0要大,故此时0,3保留。
  5. 然后来了个5,5-0>=5,故找到一组i,j,记录下来,然后判断 5-3>=5 ?如若确实大于,即再次找到一组i,j,若小于,则5保留(考虑到之后或许来了个10),依次类推

思路:

  1. 建立一个队列记录保留数字,初始为0
  2. 依次对dp中的数进行分析,如果dp[i] - dp[Q[0]] >= K,则记录一次i,j
  3. 如果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++)的更多相关文章

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

  3. leetcode 862 shorest subarray with sum at least K

    https://leetcode.com/problems/shortest-subarray-with-sum-at-least-k/ 首先回顾一下求max子数组的值的方法是:记录一个前缀min值, ...

  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】1099. Two Sum Less Than K 解题报告(C++)

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

  7. 【LeetCode】599. Minimum Index Sum of Two Lists 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:找到公共元素再求索引和 方法二:索引求和,使 ...

  8. 【LeetCode】375. Guess Number Higher or Lower II 解题报告(Python)

    [LeetCode]375. Guess Number Higher or Lower II 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...

  9. 【LeetCode】430. Flatten a Multilevel Doubly Linked List 解题报告(Python)

    [LeetCode]430. Flatten a Multilevel Doubly Linked List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: ...

随机推荐

  1. tabix 操作VCF文件

    tabix 可以对NGS分析中常见格式的文件建立索引,从而加快访问速度,不仅支持VCF文件,还支持BED, GFF,SAM等格式. 下载地址: 1 https://sourceforge.net/pr ...

  2. (转载)java排序实现

    Java实现几种常见排序方法 日常操作中常见的排序方法有:冒泡排序.快速排序.选择排序.插入排序.希尔排序,甚至还有基数排序.鸡尾酒排序.桶排序.鸽巢排序.归并排序等. 冒泡排序是一种简单的排序算法. ...

  3. nodeJs-querystring 模块

    JavaScript 标准参考教程(alpha) 草稿二:Node.js querystring 模块 GitHub TOP querystring 模块 来自<JavaScript 标准参考教 ...

  4. 重磅丨腾讯云开源业界首个 etcd 一站式治理平台 Kstone

    ​ Kstone 开源 在 CNCF 云原生基金会举办的2021年12月9日 KubeCon China大会上,腾讯云容器 TKE 团队发布了 Kstone etcd 治理平台开源项目. Kstone ...

  5. JVM——对象已“死”的判定

    主要针对Java堆和方法区 1.判断对象是否已"死" Java堆中存放着几乎所有的对象实例,垃圾回收器在对堆进行回收之前,首先应该判断这些对象哪些还"存活",哪 ...

  6. springboot+vue脚手架使用nginx前后端分离

    1.vue配置 /** * * 相对于该配置的nginx服务器请参考nginx配置文件 * */ module.exports = { // 基本路径 publicPath: '/', // 输出文件 ...

  7. 【编程思想】【设计模式】【行为模式Behavioral】备忘录模式Memento

    Python版 https://github.com/faif/python-patterns/blob/master/behavioral/memento.py #!/usr/bin/env pyt ...

  8. springboot整合jetty

    1.jetty介绍 通常我们进行Java Web项目开发,必须要选择一种服务器来部署并运行Java应用程序,Tomcat和Jetty作为目前全球范围内最著名的两款开源servlet容器,该怎么选呢. ...

  9. 【Linux】【CentOS】【FTP】FTP服务器安装与配置(vsftpd、lftp)

    [初次学习.配置的笔记,如有不当,欢迎在评论区纠正 -- 萌狼蓝天 @ 2021-12-02] 基本概念 FTP访问方式 实体账号:本地账户 来宾账户:guest 匿名登录:anonymous fp ...

  10. C# 使用modbus 读取PLC 寄存器地址

    使用的组件Nmodbus 定义参数,全局变量: //创建modbus实体对象 private static ModbusFactory modbusFactory; private static IM ...