Skip to content

Bug Report for coin-change #5911

@Mohamed-Moumni

Description

@Mohamed-Moumni

Bug Report for https://neetcode.io/problems/coin-change

In the Test Case coins=[2], amount=3, it passed during the first run, but after submission, it didn't work.

I even ran it locally and it gives the correct result

Image Image

Here's My Solution:

class Solution:
    dp = {}
    def coinChange(self, coins: List[int], amount: int) -> int:
        if amount < 0:
            return -1
        
        if amount == 0:
            return 0
        
        result = 2147483647
        for c in coins:
            if amount - c >= 0:
                if not (amount - c) in self.dp:
                    self.dp[amount - c] = self.coinChange(coins, amount - c)
                
                if self.dp[amount - c] == -1:
                    continue
                
                result = min(self.dp[amount - c] + 1, result)
        
        return -1 if result == 2147483647 else result

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions