Dynamic Programming21 sections · 916 units
Open in Course

When to Use Monotonic Queue

Pattern recognition

Look for these patterns to recognize monotonic queue improvement:

1.1. DP transition needs min/max over a sliding window: dp[i]=minj=iki1(f(j))+g(i)dp[i] = \min_{j=i-k}^{i-1}(f(j)) + g(i).

2.2. Fixed or bounded window size kk.

3.3. You're iterating through positions sequentially.

4.4. No complex dependencies between the function ff and gg. The improvement works when candidates can be "dominated." If a newer candidate is as good or better than an older one, the older one will never be chosen.