Dynamic Programming21 sections · 916 units
Open in Course

LeetCode 1696 Jump Game VI - Optimization

Recognizing the pattern

Look at the transition: maxj=iki1(dp[j])\max_{j=i-k}^{i-1}(dp[j]). This is exactly the sliding window maximum problem! The window contains the last kk DP values.

You already know how to solve that in O(1)O(1) per query using a monotonic decreasing deque. Apply it directly to the DP values. As you compute dp[i]dp[i], maintain a deque of indices sorted by decreasing dpdp values. The front gives you the maximum dpdp value in the valid range.