c++ solution


  • 0
    B
    class Solution {
    public:
        int coinChange(vector<int>& coins, int amount) {
            vector<int> dp(amount+1,-1);
            dp[0]=0;
            if(!coins.size()) return -1;
            
            for(int i=1;i<=amount;i++){
                for(int j:coins){
                    if(i-j<0 || dp[i-j]<0) continue;
                    else dp[i]=dp[i]<0 ? dp[i-j]+1:min(dp[i],dp[i-j]+1);
                }
            }
            return dp[amount];   
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.