LeetCode 862 题解

https://leetcode.com/problems/shortest-subarray-with-sum-at-least-k/description/

题目大意:求满足数组内,连续和至少为K的一个序列,使得这个序列最短,求最短的长度。

解题思路:P[x]表示数列的前缀和,考虑点 x1 ,x2 若 x1=p[x2],那么选择x2肯定来的比x1短;

考虑y1

维护一个双端队列,每次入队前 和 尾端的P[x]比较,若比原来的小则弹出原来的值;

和队首比较,若满足差值至少为K,则将队首的值出列。

 

class Solution {
    public int shortestSubarray(int[] A, int K) {
        int n = A.length;
        long[] p = new long[n+1];
        for(int i=0;i qt = new LinkedList<>();
        int res = n +1;
        for(int i=0;i<=n;i++)
        {
            System.out.println(p[i]);
            while(!qt.isEmpty() && p[i]<=p[qt.getLast()]) {
//                System.out.println("!"+" "+p[qt.getLast()]);
                qt.removeLast();
            }
            while(!qt.isEmpty() && p[i]>=p[qt.getFirst()]+K) {
//                System.out.println("!"+" "+p[qt.getFirst()]);
                res = Math.min(res, i - qt.removeFirst());
//                System.out.println(qt.getFirst()+" "+p[qt.getFirst()]);
            }
            qt.addLast(i);
        }
        return (res==n+1)?-1:res;
    }
}

你可能感兴趣的:(leetcode)