Given an integer array `nums` (values may be negative) and an integer `k`, return the length of the shortest non-empty contiguous subarray whose sum is at least `k`. If no such subarray exists, return `-1`.
Given an integer array nums (values may be negative) and an integer k, return the length of the shortest non-empty contiguous subarray whose sum is at least k. If no such subarray exists, return -1.
Example 1:
Input: {"nums":[1],"k":1}
Output: 1
Constraints:
1≤nums.length≤105
−105≤nums[i]≤105
1≤k≤109
Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result
Run code to see results.
Input
arr ={"nums":[1],"k":1}
prefix[0] = 0 stands for the empty prefix, so any subarray nums[i..j-1] sums to prefix[j] - prefix[i].
nums, k = 1
1
prefix sums (index 0..1), deque in blue
0
labels: prefix value at each index; gold = j, red = popped from back