LeetCode 322. Coin Change

2020-07-13 15:13:42 浏览数 (1)

题目

动态规划

代码语言:javascript复制
class Solution {
public:
    int dp[10005];
    int coinChange(vector<int>& coins, int amount) {
    
        
        memset(dp,-1,sizeof(dp));
        dp[0] = 0;
        for(int i=1;i<=amount;i  )
        {
             for(int j=0;j<coins.size();j  )
             {
                 if(i-coins[j]>=0 && dp[i-coins[j]]!=-1)
                 {
                     if(dp[i]==-1)
                         dp[i] = dp[i-coins[j]] 1;
                     else
                         dp[i] = min(dp[i],dp[i-coins[j]] 1);
                 }
             }
        }
        
       return dp[amount];
        
    }
};

0 人点赞