Dynamic Programming21 sections · 916 units
Open in Course

The Core Idea

Why old candidates become useless

Here's the breakthrough: if dp[j1]dp[j2]dp[j_1] \geq dp[j_2] and j1<j2j_1 < j_2, you never need j1j_1 as a candidate for minimum. Why? Because j2j_2 gives a better(or equal) value AND stays valid longer in the sliding window. When j1j_1 expires, j2j_2 is still there with a better(or equal) answer.

This means you can discard candidates that will never be optimal. You only keep the ones that might matter. The monotonic queue does this automatically by removing dominated elements.