BiruLyu of co-visitation
10/21/2017 - 12:30 AM

518. Coin Change 2(#1).java

class Solution {
    public int change(int amount, int[] coins) {
        if (amount == 0) return 1;
        if (coins == null || coins.length < 1 || amount < 0) return 0;
        int[] dp = new int[amount + 1];
        dp[0] = 1;
        for (int i = 0; i < coins.length; i++) {
            for (int j = coints[i]; j <= amount; j++) {
                dp[j] += dp[j - coints[i]];
            }
        }
        return dp[amount];
    }
}
class Solution {
    public int change(int amount, int[] coins) {
        if (amount == 0) return 1;
        if (coins == null || coins.length < 1 || amount < 0) return 0;
        int m =  coins.length + 1, n = amount + 1;
        int[][] dp = new int[m + 1][n + 1];
        //dp[0][0] = 1;
        for (int i = 1; i < m; i++) {
            dp[i][0] = 1;
            for (int j = 1; j < n; j++) {
                dp[i][j] = dp[i - 1][j] + (j >= coins[i - 1] ? dp[i][j - coins[i - 1]] : 0);
            }
        }
        return dp[m - 1][amount];
    }
}