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 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 ^ 51 <= K <= 10 ^ 9
思路
本来以为用dp来做,看了下答案,解法中并没有。使用的解法是滑动窗口,将问题重新定义为和A的前缀和有关,定义
P[i] = A[0] + A[1] + ... + A[i-1]
我们想要求的便是最小的 y-x,y>x 并且P[y] - P[x] >= K
Motivated by that equation, let opt(y) be the largest x such that P[x] <= P[y] - K. We need two key observations:
If
x1 < x2andP[x2] <= P[x1], thenopt(y)can never bex1, as ifP[x1] <= P[y] - K, thenP[x2] <= P[x1] <= P[y] - Kbuty - x2is smaller. This implies that our candidatesxforopt(y)will have increasing values ofP[x].If
opt(y1) = x, then we do not need to consider thisxagain. For if we find somey2 > y1withopt(y2) = x, then it represents an answer ofy2 - xwhich is worse (larger) thany1 - x.
opt(y)是使得当 P[x] <= P[y] - K 时 x 能取到的最大值。
1. 如果有 x1<x2 并且 P[x2]<=P[x1],那么opt(y)一定不是 x1,因为如果有P[x1] <= P[y] - K,那么 P[x2] <= P[x1] <= P[y] - K,但是 y - x2 is smaller。这表明对于opt(y)的候选x应该是在使P(x)递增的区间去找。要注意这里的P[x1]指的是从0到X1的数组元素之和,不是单单指一个x1位置上元素的值。
2. 如果opt(y1)=x, 那么不需要再次考虑x。因为如果我们找到某些y2>y1并且opt(y2)=x,那么这表明这个解答 y2-x 是比之前的解答 y1-x 是更坏的答案。
Calculate prefix sum B of list A.B[j] - B[i] represents the sum of subarray A[i] ~ A[j-1]
Deque d will keep indexes of increasing B[i].
For every B[i], we will compare B[i] - B[d[0]] with K.
public int shortestSubarray(int[] A, int K) {
int N = A.length, res = N + 1;
int[] B = new int[N + 1];
// 下面利用数组A重新构造了数组B,满足B[i+1]-B[j]=A[i]+A[i-1].....+A[j]
for (int i = 0; i < N; i++) B[i + 1] = B[i] + A[i];
Deque<Integer> d = new ArrayDeque<>();
for (int i = 0; i < N + 1; i++) {
while (d.size() > 0 && B[i] - B[d.getFirst()] >= K)
res = Math.min(res, i - d.pollFirst()); // 双端队列存的是索引
while (d.size() > 0 && B[i] <= B[d.getLast()]) d.pollLast(); // Deque d keep indexes of increasing B[i]
d.addLast(i);
}
return res <= N ? res : -1;
}
上面的出入队列顺序是这样的:首先对于每个索引i,对应的是B[i],将这个索引作为y位置来考虑,因为双端队列保持的索引是的B[i]是递增的,为了从最大处逼近K,我们从队头依次取索引出来计算:
B[i] - B[d.getFirst()]
如果比K大,那么则要找这其中距离索引i最近的那一个:
res = Math.min(res, i - d.pollFirst());
然后是队列要keep indexes of increasing B[i],索引判断当前的B[i]是否大于队列尾部的索引处的
B[i] <= B[d.getLast()
如果不能构成递增,根据之前的分析,当前y所在的位置i的最优解opt(y)一定不会是在前面递增的部分取,所以队列要从后往前一个个弹出队尾直至能和B[i]构成递增序列。
LeetCode862. Shortest Subarray with Sum at Least K的更多相关文章
- 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 ...
- [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 ...
- leetcode 862 shorest subarray with sum at least K
https://leetcode.com/problems/shortest-subarray-with-sum-at-least-k/ 首先回顾一下求max子数组的值的方法是:记录一个前缀min值, ...
- array / matrix subarray/submatrix sum
Maximal Subarray Sum : O(n) scan-and-update dynamic programming, https://en.wikipedia.org/wiki/Maxim ...
- 【LeetCode】1099. Two Sum Less Than K 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力求解 日期 题目地址:https://leetco ...
- LeetCode 1099. Two Sum Less Than K
原题链接在这里:https://leetcode.com/problems/two-sum-less-than-k/ 题目: Given an array A of integers and inte ...
- [LeetCode] Partition to K Equal Sum Subsets 分割K个等和的子集
Given an array of integers nums and a positive integer k, find whether it's possible to divide this ...
随机推荐
- CVE-2017-16995 Ubuntu16.04本地提权漏洞复现
0x01 前言 该漏洞由Google project zero发现.据悉,该漏洞存在于带有 eBPF bpf(2)系统(CONFIG_BPF_SYSCALL)编译支持的Linux内核中,是一个内存任意 ...
- windows内核提权
Windows by default are vulnerable to several vulnerabilities that could allow an attacker to execute ...
- 《Linux内核设计与实现》第5章读书笔记
第五章 系统调用 一.系统调用概述 系统调用在Linux中称为syscall,返回的值是long型变量:如果出错,C库会将错误代码写入errno全局变量(通过调用perror()函数可以把该变量翻译成 ...
- winform设计一个登录界面和修改密码的界面-自动切换窗体(问题[已解] 望一起讨论)(技术改变世界-cnblog)
http://www.cnblogs.com/IAmBetter/archive/2012/01/14/2322156.html winform设计一个登录界面和修改密码的界面-自动切换窗体(问题[已 ...
- [JLOI2014] 松鼠的新家 (lca/树上差分)
[JLOI2014]松鼠的新家 题目描述 松鼠的新家是一棵树,前几天刚刚装修了新家,新家有n个房间,并且有n-1根树枝连接,每个房间都可以相互到达,且俩个房间之间的路线都是唯一的.天哪,他居然真的住在 ...
- springboot集成Guava缓存
很久没有写博客了,这段时间一直忙于看论文,写论文,简直头大,感觉还是做项目比较舒服,呵呵,闲话不多说,今天学习了下Guava缓存,这跟Redis类似的,但是适用的场景不一样,学习下吧.今天我们主要是s ...
- 9.Android UiAutomator正则表达式的使用
一.正则表达式元字符: 1.一些常用元字符: 元字符 描述 . 表示任意一个字符 \s 空格字符(空格键.tab.换行.换页.回车) \S 非空字符串([^\s]) \d 一个数字(相当于[0-9]中 ...
- swift4.0中http连接(据于xcode9.3 URLSession)
NSURLSession是NSURLConnection的替代者,在2013年苹果全球开发者大会上(WWDC2013)随iOS7一起发布的,是对NSURLConnection进行了重构优化后的新的网络 ...
- (转)JAVA 十六个常用工具类
一. org.apache.commons.io.IOUtils closeQuietly 关闭一个IO流.socket.或者selector且不抛出异常.通常放在finally块 toString ...
- libuv移植到ios
libuv官网只提供了os x的编译方法,没有IOS的.既然os x和ios的系统内核差不多,并且编译工具都是xcode,那我们只要重新指定cpu架构,就可以编译出ios版的了. 1.安装python ...