Skip to content

Adding DP-1 (coin-change)#1997

Open
nikhylw wants to merge 1 commit into
super30admin:masterfrom
nikhylw:patch-1
Open

Adding DP-1 (coin-change)#1997
nikhylw wants to merge 1 commit into
super30admin:masterfrom
nikhylw:patch-1

Conversation

@nikhylw
Copy link
Copy Markdown

@nikhylw nikhylw commented May 4, 2026

No description provided.

@super30admin
Copy link
Copy Markdown
Owner

The Coin Change (W2_6_322_coin_change_tabulation.py)

Your solution is on the right track and correctly implements the tabulation method for the coin change problem. Here are some suggestions for improvement:

  • Instead of using 999999 as a placeholder for infinity, consider using float('inf') or amount + 1 (since the maximum number of coins cannot exceed amount). This makes the code more robust and easier to understand.
  • Improve variable names: Use n for the number of coins and m for the amount to align with common conventions. Also, dp table dimensions are (n+1) x (m+1), so you can name rows = len(coins) and cols = amount.
  • You can optimize the space complexity by using a 1D DP array. Since each row only depends on the previous row and the current row, you can use a single array of size amount+1 and update it iteratively. This reduces space from O(n * amount) to O(amount).
  • Add a check for the base case when amount is 0 at the beginning to return 0 immediately, though your current solution handles it correctly.
  • Consider adding comments to explain the DP transitions for better readability.

Here is an example of how you can write the optimized version with 1D DP:

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

Overall, your solution is correct and efficient, but with the above improvements, it can be more robust and efficient in terms of space.

VERDICT: PASS


House Robber

It appears you may have submitted code for a different problem. Please review the problem statement for "House Robber" and implement a solution specific to that problem. For House Robber, you can use a dynamic programming approach where dp[i] represents the maximum amount robbed up to house i, with choices to include or exclude the current house.

For example, the recurrence is: dp[i] = max(dp[i-1], nums[i] + dp[i-2])

Start by understanding the problem correctly and ensure your code matches the problem requirements. If you were working on Coin Change previously, make sure to switch contexts appropriately.

VERDICT: NEEDS_IMPROVEMENT

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