Dynamic Programming21 sections · 916 units
Open in Course

Practice - Maximum Points

Apply the pattern

Here's a practice problem: you have nn cards with values v1,v2,,vnv_1, v_2, \ldots, v_n. You can take cards, but if you take card ii, all cards with values vi1v_i - 1 and vi+1v_i + 1 are destroyed. get the highest total points.

This is Boredom in disguise. Count the frequency of each value. The total points from taking all cards of value xx is count[x]×xcount[x] \times x.

Now it's House Robber on the value array. Try coding it yourself. The key steps: count frequencies, find max value, run House Robber DP on the frequency array. Time: O(n+M)O(n + M) where MM is the maximum card value. Space: O(M)O(M) for the frequency array, or O(1)O(1) with state compression.