Dynamic Programming21 sections · 916 units
Open in Course

Challenge: Trace Boredom

Manual DP exercise

Trace the Boredom solution on input [1,2,1,3,2,2,2,2,3][1, 2, 1, 3, 2, 2, 2, 2, 3]. First, count frequencies: 11 appears twice, 22 appears five times, 33 appears twice. The values are count[1]×1=2count[1] \times 1 = 2, count[2]×2=10count[2] \times 2 = 10, count[3]×3=6count[3] \times 3 = 6. Now apply House Robber on values [2,10,6][2, 10, 6]. Compute dp[1]dp[1], dp[2]dp[2], dp[3]dp[3]. What's the final answer?

Answer: dp[1]=2dp[1] = 2, dp[2]=max(2,10)=10dp[2] = \max(2, 10) = 10, dp[3]=max(10,2+6)=10dp[3] = \max(10, 2 + 6) = 10. The answer is 1010. Take all the 22s.