[LeetCode] 862. Shortest Subarray with Sum at Least K

Problem

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

Solution

class Solution {
    public int shortestSubarray(int[] nums, int k) {
        if (nums == null || nums.length == 0) return -1;
        int len = nums.length, min = len+1;
        int[] sum = new int[len+1];
        for (int i = 0; i < len; i++) sum[i+1] = sum[i] + nums[i];
        Deque queue = new ArrayDeque<>();
        // pollFirst() == poll()    较早放入deque的元素 在队列顶部
        // getFirst() == peek()
        // pollLast()               最近放入deque的元素 在队列尾部
        // getLast()
        for (int i = 0; i <= len; i++) {
            // 检查最近放入的index,保证队列中新放入的index及对应的sum[index]均为递增
            // 反证:若保留worse candidate,那么在下面第二个while循环,该元素有可能
            // 中断while循环,并使得我们无法得到队列更左边的最优解
            while (queue.size() > 0 && sum[i]-sum[queue.getLast()] <= 0) {
                queue.pollLast();
            }
            // 检查较早放入的index update最小距离
            while (queue.size() > 0 && sum[i]-sum[queue.peek()] >= k) {
                min = Math.min(min, i-queue.peek());
                queue.pollFirst();
            }
            
            queue.offer(i);
        }
        return min <= len ? min : -1;
    }
}

你可能感兴趣的:(two-pointers,java,queue)