The description of the question is in the following link:
https://leetcode.com/problems/paint-house/
My code
class Solution:
def minCost(self, costs: List[List[int]]) -> int:
if len(costs) == 0:
return 0
else:
RedList = [0]*len(costs)
BlueList = [0]*len(costs)
GreenList = [0]*len(costs)
RedList[0] = costs[0][0];
BlueList[0] = costs[0][1];
GreenList[0] = costs[0][2];
for i in range(1,len(costs)):
RedList[i] = min(BlueList[i-1]+costs[i][0], GreenList[i-1]+costs[i][0])
BlueList[i] = min(RedList[i-1]+costs[i][1], GreenList[i-1]+costs[i][1])
GreenList[i]= min(RedList[i-1]+costs[i][2], BlueList[i-1]+costs[i][2])
Min1 = min(RedList[-1],BlueList[-1])
Min2 = min(Min1, GreenList[-1])
return Min2
Explanation:
Here for each house, we have three choices: blue, green or the red, for each choice, we have different minimum costs that we can have in order to choose that specific color (green, red or blue). We do not know which one would provide the best choice, so we can have three different routines: Red List gives the minimum cost for choosing red this time, and similar for other lists. For the best answer, we just need to find the minimum cost on choosing three different colors at the end of the list.