Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LCR103-零钱兑换 #4

Open
AAA611 opened this issue Aug 10, 2023 · 0 comments
Open

LCR103-零钱兑换 #4

AAA611 opened this issue Aug 10, 2023 · 0 comments
Labels

Comments

@AAA611
Copy link
Owner

AAA611 commented Aug 10, 2023

LCR103零钱兑换

function coinChange(coins: number[], amount: number): number {
    // dp[i]表示金额为 i 时,需要的最少硬币数
    const dp = Array(amount + 1).fill(Infinity)
    // base case
    dp[0]=0
    // amount:状态(金额)
    for (let i = 1; i <= amount; i++) {
        // coin数组:选择
        for (let j = 0; j < coins.length; j++) {
            if (i - coins[j] < 0) continue
            dp[i] = Math.min(dp[i - coins[j]] + 1, dp[i])
        }
    }
    return dp[amount] === Infinity ? -1 : dp[amount]
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant