862. Shortest Subarray with Sum at Least K

;  |     Back to Homepage   |     Back to Code List


class Solution {
    public int shortestSubarray(int[] nums, int k) {
        int n = nums.length;
        long[] pre = new long[n + 1];
        for (int i = 0; i < n; i++) {
            pre[i + 1] = pre[i] + nums[i];
        }
        int res = Integer.MAX_VALUE;
        LinkedList q = new LinkedList<>();
        for (int i = 0; i <= n; i++) {
            while (!q.isEmpty() && pre[i] <= pre[q.peekLast()]) {
                q.removeLast();
            }
            while (!q.isEmpty() && pre[i] - pre[q.peekFirst()] >= k) {
                res = Math.min(res, i - q.removeFirst());
            }
            q.offerLast(i);
        }
        
        return res == Integer.MAX_VALUE ? -1 : res;
    }
}