Skip to content
the poor coder

HackerEarth - The colorful street Solution

Poor Man
Published
Updated
Reading time
2 min read

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.

SAMPLE INPUT

Text
1
2
11 12 13
14 15 16

SAMPLE OUTPUT

Text
26

Explanation

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#

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)]
    print(minCost(costs))

Keep going

2020

HackerEarth - Molly And Flame Solution

Molly is in love with Sherlock. She wants to decide the nature of the relationship between her and Sherlock using the game of FLAME. FLAME stands for: {“Friendship”,”Love”,”Affection”,”Marriage”,”Enemies”} The rules of…

2020

HackerEarth - Josh and Hotel Solution

Josh owns a hotel, there are X rooms in the hotel and every room can be allocated to maximum 1 person at a time. If a customer arrives and there are no rooms available, the customer will leave the hotel without staying…

ESC