Dynamic Programming Coin Change Problems

前端 未结 2 1700
一整个雨季
一整个雨季 2020-12-10 06:00

I am having issues with understanding dynamic programming solutions to various problems, specifically the coin change problem:

\"Given a value N, if we want to make

相关标签:
2条回答
  • 2020-12-10 06:34
    1. Lets first talk about the number of ways, DP(m,n) = DP(m-1, n) + DP(m, n-Sm). This in indeed correct because either you can use the mth denomination or you can avoid it. Now you say why don't we write it as DP[i] = DP[i-d1]+DP[i-d2]+...DP[i-dn]. Well this will lead to over counting , lets take an example where n=4 m=2 and S={1,3}. Now according to your solution dp[4]=dp[1]+dp[3]. ( Assuming 1 to be a base case dp[1]=1 ) .Now dp[3]=dp[2]+dp[0]. ( Again dp[0]=1 by base case ). Again applying the same dp[2]=dp[1]=1. Thus in total you get answer as 3 when its supposed to be just 2 ( (1,3) and (1,1,1,1) ). Its so because your second method treats (1,3) and (3,1) as two different solution.Your second method can be applied to case where order matters, which is also a standard problem.
    2. Now to your second question you say that minimum number of denominations can be found out by DP[i] = Min{ DP[i-d1], DP[i-d2],...DP[i-dn] } + 1. Well this is correct as in finding minimum denominations, order or no order does not matter. Why this is linear / 1-D DP , well although the DP array is 1-D each state depends on at most m states unlike your first solution where array is 2-D but each state depends on at most 2 states. So in both case run time which is ( number of states * number of states each state depends on ) is the same which is O(nm). So both are correct, just your second solution saves memory. So either you can find it by 1-D array method or by 2-D by using the recurrence dp(n,m)=min(dp(m-1,n),1+dp(m,n-Sm)). (Just use min in your first recurrence)


      Hope I cleared the doubts , do post if still something is unclear.

    0 讨论(0)
  • 2020-12-10 06:39

    This is a very good explanation of the coin change problem using Dynamic Programming.

    The code is as follows:

    public static int change(int amount, int[] coins){
        int[] combinations = new int[amount + 1];
    
        combinations[0] = 1;
    
        for(int coin : coins){
            for(int i = 1; i < combinations.length; i++){
                if(i >= coin){
                    combinations[i] += combinations[i - coin];
                    //printAmount(combinations);
                }
            }
            //System.out.println();
        }
    
        return combinations[amount];
    }
    
    0 讨论(0)
提交回复
热议问题