Problem · Array
Shortest Subarray with Sum at Least K
Learn this problemProblem statement
Given a signed integer array nums and an integer k, return the length of the shortest nonempty contiguous subarray whose sum is at least k.
Return -1 if no such subarray exists.
Function
shortestSubarrayAtLeastK(nums: int[], k: long) → intExamples
Example 1
nums = [2,-1,2]k = 3return = 3Only the entire array reaches sum 3.
Example 2
nums = [1,2]k = 4return = -1No subarray reaches 4.
Constraints
1 <= nums.length <= 200000-10^9 <= nums[i] <= 10^91 <= k <= 10^18