Share my solution


  • 0
    public class Solution {
        public int coinChange(int[] coins, int amount) {
            if (amount == 0) return 0;
            int dp[] = new int[amount + 1];
            final int INF = 0x7ffffffe;
            for (int i = 1; i <= amount; i++) dp[i] = INF;
            for (int i = 0; i <= amount; i++) {
                for (int j = 0; j < coins.length; j++) {
                    if (i + coins[j] <= amount)
                        dp[i + coins[j]] = Math.min(dp[i + coins[j]], dp[i] + 1);
                }
            }
            return dp[amount] == INF ? -1 : dp[amount];
        }
    }

  • 0
    R
    This post is deleted!

Log in to reply
 

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