Given an integer array nums and an integer k, return the length of the shortest non empty contiguous subarray of nums whose sum is at least k. If no such subarray exists, return -1.
Constraints:
1≤ nums.length ≤105
−105≤ nums[i] ≤105
1≤ k ≤109
Input: nums = [1]-List[int], k = 1-int
Output: 1-int
Explanation: The subarray [1] has sum 1, which is at least k, so the shortest length is 1.
Input: nums = [1, 2]-List[int], k = 4-int
Output: -1-int
Explanation: No contiguous subarray reaches sum 4 ([1] = 1, [2] = 2, [1,2] = 3).
Input: nums = [2, -1, 2]-List[int], k = 3-int
Output: 3-int
Explanation: The entire array sums to 3, meeting k, and no shorter subarray reaches 3, so the answer is 3.
Input: nums = [84, -37, 32, 40, 95]-List[int], k = 167-int
Output: 3-int
Explanation: The subarray [32, 40, 95] sums to 167, so length 3 works, and no length-1 or length-2 subarray reaches 167.
Input: nums = [-28, 81, -20, 28, -29]-List[int], k = 89-int
Output: 3-int
Explanation: The subarray [81, -20, 28] sums to 89, meeting k, and no shorter contiguous subarray reaches 89.
Now, let’s take a moment to make sure you’ve correctly understood the problem. The quiz below helps you check if you’re solving the correct problem: