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
import bisect

class Solution:
    def shortestSubarray(self, A: List[int], K: int) -> int:
        cur, stack, idx, ans = 0, [0], [-1], float('inf')
        for i, x in enumerate(A):
            cur += x
            while len(stack) > 0 and stack[-1] >= cur:
                stack.pop()
                idx.pop()
            j = bisect.bisect_right(stack, cur - K)
            if j > 0:
                ans = min(ans, i - idx[j - 1])
            stack.append(cur)
            idx.append(i)
        
        if ans == float('inf'):
            return -1
        return ans
        

import bisect

class Solution:
    def shortestSubarray(self, A: List[int], K: int) -> int:
        cur, stack, idx, ans = 0, [0], [-1], float('inf')
        for i, x in enumerate(A):
            cur += x
            while len(stack) > 0 and stack[-1] >= cur:
                stack.pop()
                idx.pop()
            j = bisect.bisect_right(stack, cur - K)
            if j > 0:
                ans = min(ans, i - idx[j - 1])
            stack.append(cur)
            idx.append(i)
        
        if ans == float('inf'):
            return -1
        return ans
        
