Similar Problems

Similar Problems not available

Two Furthest Houses With Different Colors - Leetcode Solution

Companies:

LeetCode:  Two Furthest Houses With Different Colors Leetcode Solution

Difficulty: Easy

Topics: greedy array  

The problem statement is as follows:

There are N houses in a row. Each house can be painted with either red, blue or green color. The cost of painting each house with a certain color is different. You have to paint all the houses using these three colors such that no two adjacent houses have the same color. The cost of painting each house with a certain color is represented by a N x 3 cost matrix. For example, costs[0][0] is the cost of painting house 0 with color red; costs[1][2] is the cost of painting house 1 with color green, and so on... Find the minimum cost to paint all houses.

We need to find the minimum cost to paint all N houses with different colors, given the cost of painting each house with a certain color.

One solution for this problem is to use dynamic programming. We can create a 2D dp table, where dp[i][j] represents the minimum cost of painting houses from 0 to i-1, where the ith house has color j. In other words, dp[i][j] is the cost of painting the ith house with color j, plus the minimum cost of painting the previous houses with different colors. The answer will be the minimum value from dp[N][0], dp[N][1] and dp[N][2].

The recurrence relation for dp[i][j] can be written as follows:

dp[i][j] = costs[i-1][j] + min(dp[i-1][(j+1) % 3], dp[i-1][(j+2) % 3])

Here, costs[i-1][j] is the cost of painting the ith house with color j. We need to find the minimum cost of painting the previous houses with different colors. Since we cannot paint two adjacent houses with the same color, the previous house must have a different color from the current one. Hence, we need to take the minimum cost from the previous (i-1)th house, where the color is different from the current one. We can easily find this value by taking the minimum of dp[i-1][(j+1) % 3] and dp[i-1][(j+2) % 3], which represents the minimum cost of painting the previous house with the two different colors that are not equal to the current one.

The base case for dp table is dp[0][j] = 0, since there are no houses to paint.

The implementation for this solution is as follows:

class Solution:
    def minCost(self, costs: List[List[int]]) -> int:
        n = len(costs)
        dp = [[0]*3 for _ in range(n+1)]
        
        for i in range(1, n+1):
            for j in range(3):
                dp[i][j] = costs[i-1][j] + min(dp[i-1][(j+1) % 3], dp[i-1][(j+2) % 3])

        return min(dp[n][0], dp[n][1], dp[n][2])

Time Complexity: O(N), where N is the number of houses. Space Complexity: O(N), where N is the number of houses.

Overall, this problem can be solved using dynamic programming by creating a 2D dp table and finding the minimum cost of painting all houses with different colors.

Two Furthest Houses With Different Colors Solution Code

1