Dynamic Programming21 sections · 916 units
Open in Course

Challenge: Max Sum IS Reconstruction

Finding the optimal subsequence

Track predecessors to reconstruct the maximum sum increasing subsequence. When dp[i]=dp[j]+nums[i]dp[i] = dp[j] + nums[i] is optimal, set prev[i]=jprev[i] = j.

Find the index with maximum dpdp, then backtrack. Unlike length-based LIS, the maximum might not be at the last position. Scan all dpdp values to find the best ending index. Try it on [1,101,2,3,100,4,5][1, 101, 2, 3, 100, 4, 5]. The answer ends at index 44 (value 100100), not index 66 (value 55).