JAVA---Easy Version To Understand!!!!!


  • 20
    H
    public static int coinChange(int[] coins, int amount) {
    	if (coins == null || coins.length == 0 || amount <= 0)
    		return 0;
    	int[] minNumber = new int[amount + 1];
    	for (int i = 1; i <= amount; i++) {
    		minNumber[i] = Integer.MAX_VALUE;
    		for (int j = 0; j < coins.length; j++) {
    			if (coins[j] <= i && minNumber[i - coins[j]] != Integer.MAX_VALUE)
    				minNumber[i] = Integer.min(minNumber[i], 1 + minNumber[i - coins[j]]);
    		}
    	}
    	if (minNumber[amount] == Integer.MAX_VALUE)
    		return -1;
    	else
    		return minNumber[amount];
    }

  • 1
    E
    public class Solution {
    public int coinChange(int[] coins, int amount) {
        int[] change = new int[amount+1];
        Arrays.fill(change,-1);
        change[0]=0;
        
        for(int i=1;i<=amount;i++){
            boolean flag = false;
            int min = Integer.MAX_VALUE;
            for(int coin:coins){
                if(i>=coin && change[i-coin] != -1){
                    flag=true;
                    min = Math.min(min,change[i-coin]);
                }
            }
            if(flag){
                change[i]= 1+min;
            }
        }
        return change[amount];
    }
    

    }


  • 0
    S
    public class Solution {
        public int CoinChange(int[] coins, int amount) 
        {
            if(coins == null || coins.Count() == 0 || amount <=0)
                return 0;
            var dp = new int[amount+1];
            for(int i = 1; i<dp.Count(); i++)
            {
                dp[i] = int.MaxValue;
                for(int j =0; j<coins.Count(); j++)
                {
                    if(coins[j]<=i && dp[i - coins[j]] != int.MaxValue)
                    {
                        dp[i] = Math.Min(dp[i], dp[i - coins[j]]+1);
                    }
                }
            }
            
            return dp[amount] == int.MaxValue? -1 : dp[amount];
        }
    }
    

Log in to reply
 

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