Posts Coin Change II
Post
Cancel

Coin Change II

PROBLEM DESCRIPTION

You are given an integer array coins representing coins of different denominations and an integer amount representing a total amount of money.

Return the number of combinations that make up that amount. If that amount of money cannot be made up by any combination of the coins, return 0.

You may assume that you have an infinite number of each kind of coin.

The answer is guaranteed to fit into a signed 32-bit integer.

leetcode

SOLUTION

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
class Solution {
    
    public int change(int amount, int[] coins) {

        int[][] dp = new int[coins.length][amount+1];

        for(int i=0; i<dp.length; i++) Arrays.fill(dp[i], -1);

        return changeHelper(coins, amount, coins.length-1, dp);

    }

    public int changeHelper(int[] coins, int amount, int idx, int[][] dp){

        if(idx < 0) return 0;

        if(amount == 0) return 1;

        if(dp[idx][amount] == -1){
            
            //skip
            int x = changeHelper(coins, amount, idx-1, dp);

            //pick current coin if possible
            if(amount >= coins[idx]){
                x = x + changeHelper(coins, amount-coins[idx], idx, dp);
            }

            dp[idx][amount] = x;

        }

        return dp[idx][amount];

    }
}
This post is licensed under CC BY 4.0 by the author.