[LeetCode] Paint House

256. Paint House

There is a row of n houses, where each house can be painted one of three colors: red, blue, or green. The cost of painting each house with a certain color is different. You have to paint all the houses such that no two adjacent houses have the same color.

The cost of painting each house with a certain color is represented by an n x 3 cost matrix costs.

  • For example, costs[0][0] is the cost of painting house 0 with the color red; costs[1][2] is the cost of painting house 1 with color green, and so on…

Return the minimum cost to paint all houses.

1
2
3
4
5
6
7
8
9
10
11
12
class Solution {
public:
int minCost(vector<vector<int>>& costs) {
int r = 0, g = 0, b = 0;
for(auto& c : costs) {
int R = c[0], G = c[1], B = c[2];
int nr = min(g,b) + R, ng = min(r,b) + G, nb = min(r,g) + B;
r = nr, g = ng, b = nb;
}
return min({r,g,b});
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/04/16/PS/LeetCode/paint-house/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.