Dynamic Programming21 sections · 916 units
Open in Course

Codeforces 977F Consecutive Subsequence - State Definition - 1

Learn how to define the DP state for finding the longest consecutive subsequence.

I again follow the four step dynamic programming pattern.

1. Define the state (what information dp[vv] tracks)

You want a subsequence that forms: [x,x+1,x+2,,x+k1]\left[ x, x+1, x+2, \dots, x+k-1 \right] Let: dp[v]dp[v] = The maximum length of a good subsequence that ends with value vv.

That means: among all subsequences you can pick from the array that end in value v, dp[v] stores the longest length achievable. Since values in the array can be as large as 10910^9, dp must be implemented as a map/hash map, not an array. Before moving to the next unit, think that only DP can't solve the whole problem; something else is also required.