Very easily understood Cpp solution


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

Log in to reply
 

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