HackerEarth - The colorful street Solution
1 min read

HackerEarth - The colorful street Solution

HackerEarth - The colorful street Solution

There is a street by the name of a colorful street in Pretty Town. The residents of the house have decided that they will paint their houses in either Pink, Orange, or Yellow color and not other. They have also decided that no two adjacent houses will have the same color. For house i, i−1 and i+1 are the neighbors and note that the first and the last house are not neighbors.

The cost of painting a house in a particular color is different. The cost of painting the first house in color yellow may be different than what its for painting the second house in the same color.

You have to find the minimum cost of painting the houses which satisfy the given condition.

Input Constraints — Number of houses will contain between 1 and 20 elements, inclusive. — Each line of input for houses will have three values for the cost of coloring in each color. Each value will be between 1 and 1000.

Input Format — The first line will contain the number of test cases — T. — The second line will contain an integer N that will specify how many houses are there. — Each of the following N lines will contain 3 numbers separated by space that represents the cost of painting that house in either pink, orange, or yellow color.

Output Format Print T lines showing the cost of painting the houses for each test case.


11 12 13
14 15 16




The first house should be painted in pink (11), the second should be painted in orange (15). So the total cost becomes 11+15=26

Solution in Python

def minCost(costs):
    for i in range(1,len(costs)):
        x,y = costs[i],costs[i-1]
        x[0] += min(y[1], y[2])
        x[1] += min(y[0], y[2])
        x[2] += min(y[0], y[1])
    return min(costs[-1])
t = int(input())
for _ in range(t):
    n = int(input())
    costs = [list(map(int,input().strip().split())) for i in range(n)]

Enjoying these posts? Subscribe for more

Adblocker detected! Please consider reading this notice.

We've detected that you are using AdBlock Plus or some other adblocking software which is preventing the page from fully loading.

That's okay. But without advertising-income, we can't keep making this site awesome.

We don't have any banner, Flash, animation, obnoxious sound, or popup ad. We do not implement these annoying types of ads!

We need money to operate the site, and almost all of it comes from our online advertising.

Please add thepoorcoder.com to your ad blocking whitelist or disable your adblocking software.