Data Structures19 sections · 729 units
Open in Course

Lessons from Constrained Sum

DP optimization

What did you learn?

1.1. When DP transitions look at the last kk states, monotonic deques can optimize from O(nk)O(nk) to O(n)O(n).

2.2. You've learned a general pattern. Any DP where you need max\max or min\min over a sliding window of previous states can use this technique.

3.3. The deque stores indices into the DP array, ordered by DP values. This optimization appears in advanced DP problems. Recognize the pattern: "max/min over previous kk states."