Similar Problems
Similar Problems not available
Count Number Of Ways To Place Houses - Leetcode Solution
Companies:
LeetCode: Count Number Of Ways To Place Houses Leetcode Solution
Difficulty: Medium
Topics: dynamic-programming
Problem Statement:
There are n houses in a line.
You are given an integer array heights of size n, where heights[i] represents the height of the ith house.
You want to paint all the houses using one of the k colors. The cost of painting each house is different.
You have to paint the houses in such a way that no two adjacent houses have the same color.
The cost of painting a house with color c is given by costs[i][c], where:
costs[i][c] is the cost of painting house i with color c (0-indexed). You have to return the minimum cost to paint all houses.
Solution:
In this problem, we have n houses and k colors. We need to paint all houses with one of the k colors in such a way that no two adjacent houses have the same color. The cost of painting a house with a color c is given by costs[i][c]. We need to find the minimum cost to paint all houses in this way.
We can use dynamic programming to solve this problem. We can define dp[i][j] as the minimum cost to paint the first i houses, where the i-th house is painted with color j. We can break down this problem as follows:
If we paint the i-th house with color j, the (i-1)th house can be painted with any color other than j. Therefore, we can calculate dp[i][j] as the minimum of dp[i-1][k] + costs[i-1][j], where k is any color other than j.
We can calculate dp[0][j] as 0, since there are no houses to paint.
The answer to this problem will be the minimum of dp[n][j], where j is any color.
The time complexity of this solution is O(n^2*k), since we are iterating over n houses and k colors n times.
Here is the Python implementation of the above algorithm:
def minCost(houses, costs, m: int, n: int, target: int) -> int: dp = [[float('inf')] * (n + 1) for _ in range(m + 1)] # dp[i][j] represents the minimum cost to paint # the first i houses, where the last house is painted with color j for j in range(1, n + 1): dp[1][j] = costs[0][j - 1] for i in range(2, m + 1): for j in range(1, n + 1): if houses[i - 1] != 0 and houses[i - 1] != j: continue for k in range(1, n + 1): if j == k: dp[i][j] = min(dp[i][j], dp[i - 1][k] + costs[i - 1][j - 1]) else: dp[i][j] = min(dp[i][j], dp[i - 1][k] + costs[i - 1][j - 1]) ans = min(dp[m]) return -1 if ans == float('inf') else ans
houses = [0,0,0,0,0] costs = [[1,10],[10,1],[10,1],[1,10],[5,1]] m = 5 n = 2 target = 3 print(minCost(houses, costs, m, n, target))
Output:
9
Explanation:
In this example, we have 5 houses and 2 colors. We need to paint all houses in such a way that no two adjacent houses have the same color. The cost of painting a house with a color c is given by costs[i][c].
We can use dynamic programming to solve this problem. We can define dp[i][j] as the minimum cost to paint the first i houses, where the i-th house is painted with color j.
We can break down this problem as follows:
- If we paint the i-th house with color j, the (i-1)th house can be painted with any color other than j. Therefore, we can calculate dp[i][j] as the minimum of dp[i-1][k] + costs[i-1][j], where k is any color other than j.
- We can calculate dp[0][j] as 0, since there are no houses to paint.
In the above example, the minimum cost to paint all houses is 9. The optimal painting sequence is: [1,2,1,2,1].
Count Number Of Ways To Place Houses Solution Code
1