Paint House

MediumDynamic Programming1d dp

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

A street has n houses in a row, and each must be painted red, blue, or green. Painting house i with color j costs costs[i][j] (column 0 = red, 1 = blue, 2 = green). No two neighboring houses may share a color. Return the minimum total cost to paint every house under that rule. Aim for O(n) time and O(1) extra space.

Examples

Input: costs = [[17,2,17],[16,16,5],[14,3,19]]
Output: 10
Blue for house 0 (2), green for house 1 (5), blue for house 2 (3): 2 + 5 + 3 = 10.
Input: costs = [[7,6,2]]
Output: 2
A single house has no neighbors, so pick the cheapest color.

Constraints

  • `n == costs.length`
  • `1 <= n <= 100`
  • `costs[i].length == 3`
  • `0 <= costs[i][j] <= 20`

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More 1d dp problems