JAVA DP solution


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

Log in to reply
 

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