Skip to content

Conversation

@dev-08
Copy link

@dev-08 dev-08 commented Jan 25, 2026

No description provided.

@super30admin
Copy link
Owner

It seems there has been a mix-up in the solutions. You have submitted a solution for the Coin Change 2 problem instead of the Paint House problem. For the Paint House problem, you need to design a solution that minimizes the cost of painting houses with no two adjacent houses having the same color.

To solve the Paint House problem, you can use dynamic programming. The idea is to maintain the minimum cost to paint each house with each color, considering the constraints. For each house i and each color c, the cost would be the cost of painting house i with color c plus the minimum cost of painting the previous house with any color except c.

Here's a suggested approach:

  • Initialize three variables (or an array) to store the cumulative costs for the first house for each color.
  • For each subsequent house, update the cumulative cost for each color by adding the current house's cost for that color to the minimum cumulative cost from the previous house for the other two colors.
  • After processing all houses, the answer is the minimum of the three cumulative costs.

Example code in Java (similar to the reference solution but iterative):

public int minCost(int[][] costs) {
    if (costs == null || costs.length == 0) return 0;
    int n = costs.length;
    int[] prev = new int[3];
    prev[0] = costs[0][0];
    prev[1] = costs[0][1];
    prev[2] = costs[0][2];
    for (int i = 1; i < n; i++) {
        int[] curr = new int[3];
        curr[0] = costs[i][0] + Math.min(prev[1], prev[2]);
        curr[1] = costs[i][1] + Math.min(prev[0], prev[2]);
        curr[2] = costs[i][2] + Math.min(prev[0], prev[1]);
        prev = curr;
    }
    return Math.min(prev[0], Math.min(prev[1], prev[2]));
}

This solution has O(n) time complexity and O(1) space complexity (if we use variables instead of arrays for prev and curr). Please ensure you are solving the correct problem in the future.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants