Problem Description
Given an integer array nums and an integer k, return the length of the shortest non-empty contiguous subarray of nums with a sum of at least k. If there is no such subarray, return -1.
Key Insights
- Use a prefix-sum array to quickly compute the sum of any subarray.
- The difference between two prefix sums gives the sum of the subarray between their indices.
- Maintain a monotonic deque that stores indices of the prefix-sum array in increasing order.
- As you iterate through the prefix sums, check and update the answer when the difference meets or exceeds k.
- Remove indices from the deque that are no longer potential candidates (either because they yield a subarray that is too long or their prefix sum is not optimal).
Space and Time Complexity
Time Complexity: O(n) where n is the number of elements in nums. Space Complexity: O(n) due to storing the prefix sum array and the deque.
Solution
We solve the problem by first constructing a prefix-sum array where each element p[i+1] equals p[i] plus nums[i]. For any two indices i and j (with i < j), the sum of the subarray nums[i] to nums[j-1] is p[j] - p[i]. We need this difference to be at least k. By using a deque, we can efficiently track and check for the shortest valid subarray.
The deque is maintained in increasing order of prefix sums:
- As we iterate through the prefix sums, if the current prefix sum minus the smallest prefix sum from the deque is at least k, we update the minimum length.
- We remove indices from the back if they offer no benefit (i.e., having a larger prefix sum when a smaller one has already been seen).
- This ensures that we always consider the best candidate indices for forming a valid subarray.