Dynamic Programming21 sections · 916 units
Open in Course

LeetCode 1696 Jump Game VI - DP Formulation

State and transition

Let dp[i]dp[i] = maximum score to reach index ii. The base case is dp[0]=nums[0]dp[0] = nums[0]. The transition: dp[i]=nums[i]+maxj=max(0,ik)i1(dp[j])dp[i] = nums[i] + \max_{j=\max(0, i-k)}^{i-1}(dp[j]).

You take the best score from any reachable previous position and add the current element. Naive implementation: for each ii, loop through the last kk positions. That's O(nk)O(nk) time. With n=105n = 10^5 and k=105k = 10^5, you're looking at 101010^{10} operations. Time to improve.