Skip to content

Working Implementation#1981

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

Working Implementation#1981
avcode3 wants to merge 1 commit intosuper30admin:masterfrom
avcode3:master

Conversation

@avcode3
Copy link

@avcode3 avcode3 commented Feb 15, 2026

No description provided.

@super30admin
Copy link
Owner

Strengths:

  • You implemented the dynamic programming solution correctly, which is the standard approach for this problem.
  • Your solution has optimal time complexity (O(m*n)), which is efficient for the given constraints.

Areas for improvement:

  • Avoid using magic numbers like 99999. Instead, use a value that is clearly defined, such as amount + 1 (since the maximum number of coins cannot exceed amount) or float('inf'). This makes the code more robust and readable.
  • Consider optimizing the space complexity from O(m*n) to O(n) by using a 1D DP array. This would reduce memory usage without affecting time complexity.
  • Use more descriptive variable names. For example, instead of my_arr, use dp or coin_change_table to make the code self-documenting.
  • Although not necessary, adding comments to explain the base cases and the recurrence relation would be helpful for others reading your code.

For the DP solution with space optimization, you can do:

def coinChange(self, coins: List[int], amount: int) -> int:
    dp = [amount + 1] * (amount + 1)
    dp[0] = 0
    for coin in coins:
        for j in range(coin, amount + 1):
            dp[j] = min(dp[j], 1 + dp[j - coin])
    return dp[amount] if dp[amount] != amount + 1 else -1

This approach uses a 1D array and iterates over each coin and then over amounts from the coin value to the target amount.

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