Dynamic Programming Coin Change Problems

前端 未结 2 1710
一整个雨季
一整个雨季 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: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];
    }
    

提交回复
热议问题