LeetCode 518. Coin Change 2

一看就是用dp来做,但是这道题还是有点意思的。

dp[i] = sum_{coin} dp[i-coin]

上述的递推公式看似很对,但是会把重复的情况都考虑进去。举个例子,算dp[5]的时候,dp[5]=dp[0]+dp[4]+dp[3],但是之前算的dp[4]就是从dp[3]计算得到的。

解决办法是,一个一个硬币更新dp,即 dp[i] += dp[i-coin] for each coin。这样,对于当前的coin,dp[i-coin] 一定是不会包含当前coin的。

class Solution {
public:
    int change(int amount, vector<int>& coins) {
        vector<int> dp(amount+1,0);
        dp[0] = 1;
        for (auto coin:coins){
            for (int i=1;i<=amount;++i){
                if (i>=coin) dp[i]+=dp[i-coin];
            }
        }
        return dp[amount];
    }
};

原文地址:https://www.cnblogs.com/hankunyan/p/9937405.html

时间: 2024-10-15 08:11:28

LeetCode 518. Coin Change 2的相关文章

leetcode 518. Coin Change 2/硬币找零 2

归纳于http://www.cnblogs.com/grandyang/p/7669088.html 原题https://leetcode.com/problems/coin-change-2/description/ 518. Coin Change 2(Medium) You are given coins of different denominations and a total amount of money. Write a function to compute the numbe

[LeetCode][JavaScript]Coin Change

Coin Change You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination o

【Leetcode】Coin Change

题目链接:https://leetcode.com/problems/coin-change/ 题目: You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of mone

518. Coin Change 2

coin循环要在外面 从小到大 不会有重复 1 class Solution { 2 public int change(int amount, int[] coins) { 3 // if(coins.length == 0) return 1; 4 int[] dp = new int[amount + 1]; 5 dp[0] = 1; 6 Arrays.sort(coins); 7 for(int j = 0; j < coins.length; j++){ 8 for(int i = 1

(Java) LeetCode 322. Coin Change —— 零钱兑换

You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins,

LeetCode OJ 322. Coin Change DP求解

题目链接:https://leetcode.com/problems/coin-change/ 322. Coin Change My Submissions Question Total Accepted: 15289 Total Submissions: 62250 Difficulty: Medium You are given coins of different denominations and a total amount of money amount. Write a func

Leetcode OJ --- 322. Coin Change

You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins,

dp背包问题/01背包,完全背包,多重背包,/coin change算法求花硬币的种类数

一步一步循序渐进. Coin Change 具体思想:给你 N元,然后你有几种零钱S={S1,S2...,Sm} (每种零钱数量不限). 问:凑成N有多少种组合方式  即N=x1 * S1+x2*S2+...+xk*Sk (xk>=0,k=1,2..m) 设有f(x)中组合方式 有两种解答(自底向上回溯): 1.不用第m种货币   f(N,m-1) 2.用第m种货币 f(N-Sm,m) 总的组合方式为f(N,m)=f(N,m-1)+f(N-Sm,m) anything is nonsense,s

HDU 2069 Coin Change

Coin Change Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID: 206964-bit integer IO format: %I64d      Java class name: Main Suppose there are 5 types of coins: 50-cent, 25-cent, 10-cent, 5-cent, and 1-cent. We