Problem
FAANG · LeetCode #862

Shortest Subarray with Sum at Least K — three-stage drill

1The ProblemHard

Shortest Subarray with Sum at Least K

Shortest subarray with sum ≥ k — but values can be negative.

shortest subarray with sum ≥4negatives allowed — a sliding window won't work30-2152[5] = 5 ≥ 4, length 1return1
Examples
[3,-2,5], k=4→ 1[1,2], k=4→ -1

constraintValues may be negative.

2The PatternTIME O(n) · SPACE O(n)

Monotonic Deque

A deque kept sorted by popping the back — the front is always the best candidate.

keep the deque monotonic — pop the back before pushing a smaller valuefrontback1358incoming 4 · back 8 ≥ 4 → pop back
Problems using this pattern
3The SolutionRESULT → 1

Step through it

k = 4↩ ANSWERshortest =—P00311263ideque — indices, shown by their P valuefront00
i=0 (P=0): deque empty → push index 0
Step 1 / 8
Arrow keys to navigate · escape to reset

All problems by pattern

Backtracking

Recursive Descent