This is basically the paint house problem but generalized to C colors.
Start by defining the DP state: dp[i][c] = min cost to color items i..H-1 given item i has color c. Then present both top-down memoization and bottom-up space-optimized DP, and finally show how to reconstruct the coloring using parent pointers or by re-deriving choices.
Pro tip: Emphasize the space optimization: since dp[i] depends only on dp[i+1], you can reduce space from O(H*C) to O(C) while still reconstructing the solution by storing choices or using a backward pass.
Clearly state dp[i][c] as the minimum cost to color items i through H-1 with item i assigned color c. Write the recurrence: dp[i][c] = cost[i][c] + min_{c' != c} dp[i+1][c'].
Use recursion with a memo table (H x C) to avoid recomputation. Base case: i == H returns 0. For each state, iterate over all colors except c to find the minimum.
Iterate i from H-1 down to 0, maintaining only the next row's dp values. For each color c, compute dp[i][c] using the minimum of the next row excluding c. Track the overall minimum at i=0.
During bottom-up, store the chosen color for each i and c (or recompute by comparing costs). Then backtrack from i=0 to H-1 to output the color sequence.
Time: O(H * C^2) naive, but can be optimized to O(H * C) by tracking the two smallest values in the next row. Space: O(H*C) for top-down, O(C) for bottom-up with reconstruction.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.