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 ...
随机推荐
- 学习React中遇到的问题
1.执行eject后,再次启动项目报错 情景:使用create-react-app搭建了项目,启动没有问题,然后执行 $ yarn eject 暴露出webpack配置文件等,再次 $ yarn st ...
- android 透明弹出搜索框
1.在QQ一些APP当中有是弹出一个半透明的搜索框的,其实这种效果就是很多种方法,自定义一个Dialog,或者直接将activity的背景变成半透明的也可以的. 下面就是将activity变成半透明的 ...
- .NET架构转Java开发必须了解的历史
终于不在职守在.NET领域 .NET的winform和webform项目也开发了很多了 尤其是WEB领域 从ASP -> ASP.NET 2.0 -> ASP.NET MVC 4.0 - ...
- 在Linux环境下的卸载Oracle11G操作
1.使用SQL*PLUS停止数据库[oracle@OracleTest oracle]$ sqlplus /nolog SQL> connect / as sysdba SQL> shut ...
- cinder侧挂载卷流程分析
cinder侧挂载卷流程分析,存储类型以lvm+iscsi的方式为分析基础cinder侧主要调用了三个接口1)reserve_volume: 把volume的状态改为attaching,阻止其它节点执 ...
- Javascript判断两个点(经纬度)的距离,以及是否在某个区域内(经纬度字符串)
JS计算两个点(经纬度)的距离 function getGreatCircleDistance(lat1, lng1, lat2, lng2) { var EARTH_RADIUS = 6378137 ...
- ClickOnce发布包含某文件
第一步.在文件上右键选择“属性”,“复制到输出目录”选择“始终复制”: 第二步.“生成操作”选择“选择”: 第三步.通过 项目右键属性-发布-应用程序文件 查看想要包含的文件是否包含进来了. 注:可以 ...
- python3使用newspaper快速抓取任何新闻文章正文
newspaper用于爬取各式各样的新闻网站 1,安装newspaper pip install newspaper3k 2,直接上代码 from newspaper import Article u ...
- spark-2.2.1在centos7安装
前言 在安装Spark之前,我们需要安装Scala语言的支持.在此我选择的是scala-2.11.12版本.jdk8也要保证已经安装好并且配置好环境变量 scala-2.11.12下载 为了方便,我先 ...
- django 部署到Ubuntu安装MYSQL56
阿里云 Ubuntu 14.04 安装mysql 5.6 1.升级apt-get sudo apt-get update 2. 安装mysql5.6版本 apt-get install mysql-s ...