Dynamic Programming21 sections · 916 units
Open in Course

LeetCode 300 Longest Increasing Subsequence - State Design

What dp[i] means

Define dp[i]dp[i] as the length of the longest increasing subsequence ending at index ii. Why "ending at ii" instead of "using elements 0 to ii"? Because when we extend a subsequence, we need to know its last element.

If dp[i]dp[i] just meant "best in prefix", we wouldn't know what value to compare against. With the "ending at" definition, arr[i]arr[i] is the last element. We can check if arr[j]<arr[i]arr[j] < arr[i] for all j<ij < i and extend from there. Base case: dp[i]=1dp[i] = 1 for all ii. Every element by itself is a valid increasing subsequence.