Minimize cost of painting N houses such that adjacent houses have different colors | C # program for the above approach ; Function to find the minimum cost of coloring the houses such that no two adjacent houses has the same color ; Corner Case ; Auxiliary 2D dp array ; Base Case ; If current house is colored with red the take min cost of previous houses colored with ( blue and green ) ; If current house is colored with blue the take min cost of previous houses colored with ( red and green ) ; If current house is colored with green the take min cost of previous houses colored with ( red and blue ) ; Print the min cost of the last painted house ; Driver Code ; Function Call
using System ; using System . Collections . Generic ; class GFG { static int minCost ( List < List < int > > costs , int N ) { if ( N == 0 ) return 0 ; List < int > temp = new List < int > ( ) ; for ( int i = 0 ; i < 3 ; i ++ ) temp . Add ( 0 ) ; List < List < int > > dp = new List < List < int > > ( ) ; for ( int i = 0 ; i < N ; i ++ ) dp . Add ( temp ) ; dp [ 0 ] [ 0 ] = costs [ 0 ] [ 0 ] ; dp [ 0 ] [ 1 ] = costs [ 0 ] [ 1 ] ; dp [ 0 ] [ 2 ] = costs [ 0 ] [ 2 ] ; for ( int i = 1 ; i < N ; i ++ ) { dp [ i ] [ 0 ] = Math . Min ( dp [ i - 1 ] [ 1 ] , dp [ i - 1 ] [ 2 ] ) + costs [ i ] [ 0 ] ; dp [ i ] [ 1 ] = Math . Min ( dp [ i - 1 ] [ 0 ] , dp [ i - 1 ] [ 2 ] ) + costs [ i ] [ 1 ] ; dp [ i ] [ 2 ] = Math . Min ( dp [ i - 1 ] [ 0 ] , dp [ i - 1 ] [ 1 ] ) + costs [ i ] [ 2 ] ; } return ( Math . Min ( dp [ N - 1 ] [ 0 ] , Math . Min ( dp [ N - 1 ] [ 1 ] , dp [ N - 1 ] [ 2 ] ) ) ) - 11 ; } public static void Main ( ) { List < List < int > > costs = new List < List < int > > ( ) ; costs . Add ( new List < int > ( ) { 14 , 2 , 11 } ) ; costs . Add ( new List < int > ( ) { 11 , 14 , 5 } ) ; costs . Add ( new List < int > ( ) { 14 , 3 , 10 } ) ; int N = 3 ; Console . WriteLine ( ( int ) ( minCost ( costs , N ) ) ) ; } }