Dynamic Programming21 sections · 916 units
Open in Course

Challenge: LIS Reconstruction

Finding the actual subsequence

The DP gives you the length, but which elements form the LIS? Track predecessors. When you update dp[i]=dp[j]+1dp[i] = dp[j] + 1, record prev[i]=jprev[i] = j.

This creates a chain of predecessors. Find the index with maximum dpdp value. Follow prevprev pointers backward, collecting elements. Reverse at the end. For [10,9,2,5,3,7,101,18][10, 9, 2, 5, 3, 7, 101, 18]: start at index 66 (value 101101), follow back through 5,3,25, 3, 2 to get [2,5,7,101][2, 5, 7, 101].