Shortest Subarray with Sum at Least K
Hard
LC #862
prefix summonotonic dequesliding windowNot attempted yet
Given an integer array nums (which may contain
negative numbers) and a positive integer k, return
the length of the shortest non-empty contiguous
subarray whose sum is at least k. Return -1 if
there is none.
Example 1
Input: nums = [1], k = 1
Output: 1
Example 2
Input: nums = [1, 2], k = 4
Output: -1
Example 3
Input: nums = [2, -1, 2], k = 3
Output: 3
Only the whole array reaches 3.
Constraints
- 1 ≤ len(nums) ≤ 10⁵
- -10⁵ ≤ nums[i] ≤ 10⁵
- 1 ≤ k ≤ 10⁹