Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Logic:
- dp array rows represents houses and 3 columns for red,blue and green colors.
- dp[i][j] will store the minimum cost by coloring ith house by jth color ie. j=0->red,j=1->blue,j=2->green
- coloring of first house with jth colors will be costs[0][j]
- so,if we color ith house with red,then minimum cost till ith house will be min of cost of previous house
- colored with blue and green because same color can't be in adjacent house and same goes for other colors also.
- finally,in last row,we will have minimum cost coloring last house with red,blue and green,return their minimum.
- */
- from math import inf
- class Solution:
- def distinctColoring (self, n, r, g, b):
- dp=[[-1]*3 for i in range(n)]
- dp[0][0]=r[0]
- dp[0][1]=g[0]
- dp[0][2]=b[0]
- ans=inf
- for i in range(1,n):
- dp[i][0]=r[i]+min(dp[i-1][1], dp[i-1][2]) #dp[i-1][]
- dp[i][1]=g[i]+min(dp[i-1][0], dp[i-1][2])
- dp[i][2]=b[i]+min(dp[i-1][0], dp[i-1][1])
- if i==n-1:
- ans=min([dp[i][0],dp[i][1],dp[i][2]])
- # print(dp)
- return ans
Advertisement
Add Comment
Please, Sign In to add comment