Short and Clean Java DP solution 23ms


  • 1
    M
    public int coinChange(int[] coins, int amount) {
        int[] res = 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 (coins[j] <= i && res[i - coins[j]] != -1) {
                    min = Math.min(min, res[i - coins[j]] + 1);
                }
            }
            if (min == Integer.MAX_VALUE) {
                res[i] = -1;
            } else {
                res[i] = min;
            }
        }
        return res[amount];
    }

Log in to reply
 

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