> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Shortest Subarray with Sum at Least K

> Tested Python solution for LeetCode 862 with 16 pytest cases. Generate a practice environment with lcpy.

LeetCode 862, [Hard](/catalog/hard). Topics: [Array](/catalog/topics/array), [Binary Search](/catalog/topics/binary-search), [Queue](/catalog/topics/queue), [Sliding Window](/catalog/topics/sliding-window), [Heap (Priority Queue)](/catalog/topics/heap-priority-queue), [Prefix Sum](/catalog/topics/prefix-sum), Monotonic Queue. [View on LeetCode](https://leetcode.com/problems/shortest-subarray-with-sum-at-least-k/description/).

Generate this problem as a practice environment: tested reference solution, 16 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 862   # by problem number
lcpy gen -s shortest_subarray_with_sum_at_least_k   # by problem name
```

## Problem

Given an integer array \<code>nums\</code> and an integer \<code>k\</code>, return \<em>the length of the shortest non-empty \<strong>subarray\</strong> of \</em>\<code>nums\</code>\<em> with a sum of at least \</em>\<code>k\</code>. If there is no such \<strong>subarray\</strong>, return \<code>-1\</code>.\</p>

\<p>A \<strong>subarray\</strong> is a \<strong>contiguous\</strong> part of an array.

### Examples

```
Input: nums = [1], k = 1
Output: 1
```

```
Input: nums = [1,2], k = 4
Output: -1
```

```
Input: nums = [2,-1,2], k = 3
Output: 3
```

### Constraints

* 1 \<= nums.length \<= 10^5
* -10^5 \<= nums\[i] \<= 10^5
* 1 \<= k \<= 10^9

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/shortest_subarray_with_sum_at_least_k/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/shortest_subarray_with_sum_at_least_k/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
from collections import deque


class Solution:
    # Time: O(n)
    # Space: O(n)
    def shortest_subarray(self, nums: list[int], k: int) -> int:
        n = len(nums)
        prefix = [0] * (n + 1)
        for i, num in enumerate(nums):
            prefix[i + 1] = prefix[i] + num
        result = n + 1
        queue: deque[int] = deque()
        for j in range(n + 1):
            while queue and prefix[j] - prefix[queue[0]] >= k:
                result = min(result, j - queue.popleft())
            while queue and prefix[queue[-1]] >= prefix[j]:
                queue.pop()
            queue.append(j)
        return result if result <= n else -1
```

## Complexity

| Time | Space |
| - | - |
| O(n) | O(n) |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.