Problem Statement: There are a row of houses. Each house can be painted with three colors:
red, blue and 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. You have to paint the houses with
minimum cost.
On reading the problem statement, the first approach that comes to mind is GREEDY approach. As soon as we see "minimum" keyword, we start thinking in greedy way as we try to cut down our expenses in order to save more but that is not always the case.
Let us take an example, there are 4 houses and the color cost chart is as below:
The thought process works like we start to pick the H1 with Blue i.e. 8, then H2 with Red i.e. 5, then h3 with Green i.e. 14 and finally H4 with Red i.e. 7 making the total up to 34.
But if i say H1 with Red (12), H2 with Blue (3), H3 with Red (8) and H4 with Blue (10)
making a total of 33
So my greedy approach is failed!.
So next thing comes to muy mind is If i start with H2 and Blue, I might reach the minimum but again it will fail if the cost of coloring house H4 with Red would have been 5, then the cost using the first approach would have been 32.
So the problem we face is we do not know which house to consider as the start so that i get the minimum total.
lets formulate a mathematical formula to find the total cost of painting till house X
we will keep a 2D array, which store the total minimum cost if the house would be painted with Red, Green or blue.
so the intermediate state looks something like this,
Hx-1 store the information that if Hx-1 would be painted with Red, the minimum cost till now will be 5, if with Green minimum cost will be 14 and if with Blue minimum cost will be 3.
then for Hx, if painted with Red, cost will be 8(cost off painting with Red) + 3 (minimum cost till Hx-1 with blue which is the lowest among Green and Blue)
similarly, if painted with Green cost would be 14+3
and, if painted with Blue cost would be 15+5
so the formula becomes,
coloring House Hx with Red = Hx(Red) + Min( Hx-1(Green) , Hx-1(Blue) )
and find the minimum of cost among the three color and that is the answer.
On reading the problem statement, the first approach that comes to mind is GREEDY approach. As soon as we see "minimum" keyword, we start thinking in greedy way as we try to cut down our expenses in order to save more but that is not always the case.
Let us take an example, there are 4 houses and the color cost chart is as below:
Red
|
Green
|
Blue
|
|
H1
|
12
|
19
|
8
|
H2
|
5
|
14
|
3
|
H3
|
8
|
14
|
15
|
H4
|
7
|
12
|
10
|
The thought process works like we start to pick the H1 with Blue i.e. 8, then H2 with Red i.e. 5, then h3 with Green i.e. 14 and finally H4 with Red i.e. 7 making the total up to 34.
But if i say H1 with Red (12), H2 with Blue (3), H3 with Red (8) and H4 with Blue (10)
making a total of 33
So my greedy approach is failed!.
So next thing comes to muy mind is If i start with H2 and Blue, I might reach the minimum but again it will fail if the cost of coloring house H4 with Red would have been 5, then the cost using the first approach would have been 32.
So the problem we face is we do not know which house to consider as the start so that i get the minimum total.
lets formulate a mathematical formula to find the total cost of painting till house X
we will keep a 2D array, which store the total minimum cost if the house would be painted with Red, Green or blue.
Red
|
Green
|
Blue
|
|
-
|
-
|
-
|
-
|
Hx-1
|
5
|
14
|
3
|
Hx
|
8+3
|
14+3
|
15+5
|
-
|
-
|
-
|
-
|
so the intermediate state looks something like this,
Hx-1 store the information that if Hx-1 would be painted with Red, the minimum cost till now will be 5, if with Green minimum cost will be 14 and if with Blue minimum cost will be 3.
then for Hx, if painted with Red, cost will be 8(cost off painting with Red) + 3 (minimum cost till Hx-1 with blue which is the lowest among Green and Blue)
similarly, if painted with Green cost would be 14+3
and, if painted with Blue cost would be 15+5
so the formula becomes,
coloring House Hx with Red = Hx(Red) + Min( Hx-1(Green) , Hx-1(Blue) )
and find the minimum of cost among the three color and that is the answer.