<jupyter_start><jupyter_text>返回 A 的最短的非空连续子数组的长度，该子数组的和至少为 K 。    如果没有和至少为 K 的非空子数组，返回 -1 。示例 1：    输入：A = [1], K = 1    输出：1示例 2：    输入：A = [1,2], K = 4    输出：-1示例 3：    输入：A = [2,-1,2], K = 3    输出：3 提示：    1 <= A.length <= 50000    -10 ^ 5 <= A[i] <= 10 ^ 5    1 <= K <= 10 ^ 9<jupyter_code>from collections import deque

class Solution:
    def shortestSubarray(self, A, K: int) -> int:
        n = len(A)
        A.insert(0, 0)
        presum = [0]
        for i in range(1, n+1):
            presum.append(presum[-1] + A[i])
        
        dq = deque([0])
        ret = float('inf')
        for i in range(1, n+1):
            idx = -float('inf')
            while dq and presum[i] - presum[dq[0]] >= K:
                idx = dq.popleft()
            ret = min(ret, i - idx)
            while dq and presum[dq[-1]] >= presum[i]:
                dq.pop()
            dq.append(i)
        return ret if ret != float('inf') else -1
solution = Solution()
solution.shortestSubarray(A = [2,-1,2], K = 3)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1681159420289855, 'fraction_numerical': 0.0463768115942029, 'mean_word_length': 1.7924528301886793, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '628060', 'license': 'Apache-2.0', 'n_tokens_mistral': 466, 'n_tokens_neox': 448, 'n_words': 108, 'path': 'Dynamic Programming/1118/862. Shortest Subarray with Sum at Least K.ipynb', 'repo_name': 'YuHe0108/Leetcode', 'size': '1035'}