Easy To Understand Java 25ms AC Solution!!!


  • 0
    G
    public int coinChange(int[] coins, int amount) {
            if (amount == 0) return 0;
            int[] dp = new int[amount + 1];
            Arrays.sort(coins);
            for (int i = 0; i <= amount; ++i) {
                dp[i] = i % coins[0] == 0? i / coins[0] : Integer.MAX_VALUE - 1;
            }
            for (int i = 1; i < coins.length; ++i) {
                for (int j = 1; j <= amount; ++j) {
                    if (j >= coins[i]) dp[j] = Math.min(1 + dp[j - coins[i]], dp[j]);
                    else dp[j] = dp[j];
                }
            }
            return dp[amount] == Integer.MAX_VALUE - 1? -1 : dp[amount];
        }

Log in to reply
 

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