Dynamic Programming21 sections · 916 units
Open in Course

LeetCode 862 Shortest Subarray with Sum at Least K - Problem Statement

LeetCode 862

Given an integer array nums (can have negative numbers) and integer kk, find the length of the shortest non-empty subarray with sum at least kk. Return -1 if no such subarray exists. Example: nums = [2, -1, 2], k = 3. The subarray [2, -1, 2] has sum 3 and length 3. That's the shortest.

Answer: 3. This problem is tricky because negative numbers break the sliding window approach. You can't just shrink from the left when the sum exceeds kk. This requires a different kind of monotonic queue.