Skip to content

Solved DP-2#1796

Open
pratul2789 wants to merge 1 commit intosuper30admin:masterfrom
pratul2789:master
Open

Solved DP-2#1796
pratul2789 wants to merge 1 commit intosuper30admin:masterfrom
pratul2789:master

Conversation

@pratul2789
Copy link

No description provided.

@super30admin
Copy link
Owner

Strengths:

  • The Paint House solution is correct and efficient.
  • The code is well-structured and readable with clear comments.
  • The time and space complexity are optimal for the problem.

Areas for Improvement:

  • The submission includes an unrelated solution (Coin Change 2). When submitting code, ensure that only the relevant solution is provided to avoid confusion.
  • For the Paint House solution, you can optimize space further by using only two arrays (for the previous and current house) instead of storing all houses. This would reduce space complexity to O(1).
  • Consider adding comments to explain the DP state: what dp[i][j] represents (the minimum cost to paint house i with color j).

Suggested optimized solution for space:

class Solution(object):
    def minCost(self, costs):
        if not costs:
            return 0
        prev = costs[0]
        for i in range(1, len(costs)):
            current = [0, 0, 0]
            current[0] = costs[i][0] + min(prev[1], prev[2])
            current[1] = costs[i][1] + min(prev[0], prev[2])
            current[2] = costs[i][2] + min(prev[0], prev[1])
            prev = current
        return min(prev)

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