C solution --min coin change


  • 0
    S

    int minof(int a, int b){

    return a>b?b:a;
    

    }
    int coinChange(int* coins, int coinsSize, int amount) {

    int dp[amount+1];
    
    dp[0]=0;
    
    
    int i,j;
    
    for(i=1;i<=amount;i++)
        dp[i]=INT_MAX-1;
    
    
       for(j=0;j<coinsSize;j++){
            for(i=1;i<=amount;i++){
            	if(i-coins[j]>=0)
                 dp[i]=minof(1+dp[i-coins[j]],dp[i]);
    
    
    
        }
    
    }
    if(dp[amount]==INT_MAX-1)
        return -1;
    else 
        return dp[amount];
    

    }


Log in to reply
 

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