Clean and understandable java solution


  • 0
    W
    public int coinChange(int[] coins, int amount) {
        if(coins == null || amount < 0)
            return -1; 
        if(amount == 0)
            return 0;
        int[] dp = new int[amount+1];
        for(int i = 1; i <=amount; i++){
            int min = Integer.MAX_VALUE;
            for(int j = 0; j < coins.length; j++){
                if(i >= coins[j] && dp[i - coins[j]] != -1)
                    min = Math.min(min, 1 + dp[i - coins[j]]);
            }
            dp[i] = min == Integer.MAX_VALUE ? -1 : min;
        }
        return dp[amount];
    }

Log in to reply
 

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