My easy Cpp Solution


  • 0
    K

    class Solution {

    public:

    int min(int a,int b){
        return a<b?a:b;
    }
    int calculate(vector<int>& coins, int amount,int count[]){
        int coinsCount=coins.size(),i;
        for(i=0;i<coinsCount;i++){
            int j;
            for(j=0;j<=amount;j++){
                if(j-coins[i]>=0)
                    count[j] = min(count[j],1+count[j-coins[i]]);
                //cout<<count[j]<<" ";
            }
            //cout<<endl;
        }
        if(count[amount]==INT_MAX-1)
            return -1;
        return count[amount];
    }
    int coinChange(vector<int>& coins, int amount) {
        int count[amount+1]={0};
        int i;
        for(i=1;i<=amount;i++)
            count[i]=INT_MAX-1;
        int index[amount+1];
        return calculate(coins,amount,count);
    }
    

    };


Log in to reply
 

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