Dynamic Programming21 sections · 916 units
Open in Course

LeetCode 300 Longest Increasing Subsequence - Transition

The recurrence

To compute dp[i]dp[i], look at all indices j<ij < i. If arr[j]<arr[i]arr[j] < arr[i], you can extend the subsequence ending at jj by adding arr[i]arr[i]. The formula (how dp[i] depends on earlier dp values): dp[i]=max(dp[j]+1)dp[i] = \max(dp[j] + 1) for all j<ij < i where arr[j]<arr[i]arr[j] < arr[i] If no such jj exists, dp[i]dp[i] stays at 1.

The final answer is max(dp[i])\max(dp[i]) for all ii, not just dp[n1]dp[n-1]. The longest subsequence might end anywhere. This formula captures how dp[i] builds on dp[j] for earlier indices.