Skip to content

Instantly share code, notes, and snippets.

@kuldeeprishi
Created December 16, 2018 03:45
Show Gist options
  • Save kuldeeprishi/b7feaa94d7a4f250388e1e3443254035 to your computer and use it in GitHub Desktop.
Save kuldeeprishi/b7feaa94d7a4f250388e1e3443254035 to your computer and use it in GitHub Desktop.
The colorful street
import sys
def find_cost():
testcases = int(sys.stdin.readline())
for x in range(testcases):
num_of_houses = int(sys.stdin.readline())
cost_of_color = cost_table(num_of_houses)
dp = [[0 for i in range(3)] for i in range(num_of_houses)]
for i in range(3):
dp[0][i] = cost_of_color[0][i]
for i in range(1, num_of_houses):
dp[i][0] = min( dp[i-1][1] , dp[i-1][2] ) + cost_of_color[i][0]
dp[i][1] = min( dp[i-1][0] , dp[i-1][2] ) + cost_of_color[i][1]
dp[i][2] = min( dp[i-1][0] , dp[i-1][1] ) + cost_of_color[i][2]
print min(min(dp[num_of_houses-1][0], dp[num_of_houses-1][1]) , dp[num_of_houses-1][2])
return 0
def cost_table(num_of_houses):
'''creates a list of n list where n is num_of_houses'''
cost_of_color = [[0 for i in range(3)] for i in range(num_of_houses)]
for i in range(num_of_houses):
cost_of_color[i] = map(int, (sys.stdin.readline()).split())
return cost_of_color
find_cost()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment