Dsa
Dynamic Programming
Hard
Min Cost to Paint Houses
You are given a row of n houses, where each house can be painted in one of three colors: Red, Green, or Blue. The cost of painting house i with color j is given in a 2D array 'costs', where costs[i][j] is the cost of painting house i with color j. You want to paint all houses such that no two adjacent houses have the same color. Write a program that calculates the minimum cost to paint all houses while adhering to this constraint.
Input:
- The first line contains an integer n, the number of houses.
- The next n lines each contain three integers representing the costs of painting the house in Red, Green, and Blue respectively.
Output:
- A single integer representing the minimum cost to paint all houses.
Example:
Input:
3
17 2 17
16 16 5
14 3 19
Output:
10
Key concepts
dynamic_programmingarraysgreedy
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now