Easy DP solution - Java


  • 0
    S
    public int coinChange(int[] coins, int amount) {
            
            int[] dp = new int[amount + 1];
            Arrays.fill(dp, amount+1);
            dp[0] = 0;
            
            for (int coin : coins) 
            {
                for (int i = coin; i <= amount; i++) 
                {
                    dp[i] = Math.min(dp[i], dp[i - coin] + 1);
                }
            }
            
            return dp[amount] > amount ? -1 : dp[amount];
        }
    

Log in to reply
 

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