JAVA DP solution


  • 0
    T

    public class Solution {
    public int coinChange(int[] coins, int amount) {

        int[] table = new int[amount+1];
        for (int i = 0; i < amount + 1; i++)
            table[i]  = -1;
        table[0] = 0;
        for (int i = 1; i < amount + 1; i++){
            for (int j = 0; j < coins.length; j++){
                if (i - coins[j] >= 0 && table[i-coins[j]] > -1 && (table[i] == -1 || table[i] > (table[i - coins[j]] + 1))){
                    table[i] = table[i - coins[j]] + 1;
                }
            }
        }
        return table[amount];
    }
    

    }


  • 0
    A

    As much I dont understand the code (yet), the code doesn't run on the website. Just simply try the example inputs [1 2 5] 11 and you will see there is a runtime error. That happens to most dp solutions. Am I missing something here?


Log in to reply
 

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