Find the least number of coins required that can make any change from 1 to 99 cents

后端 未结 27 2250
生来不讨喜
生来不讨喜 2020-12-07 10:08

Recently I challenged my co-worker to write an algorithm to solve this problem:

Find the least number of coins required that can make any change from

27条回答
  •  一个人的身影
    2020-12-07 10:37

    I wrote this algorithm for similar kind of problem with DP, may it help

    public class MinimumCoinProblem {
    
        private static void calculateMinumCoins(int[] array_Coins, int sum) {
    
            int[] array_best = new int[sum];
    
            for (int i = 0; i < sum; i++) {
                for (int j = 0; j < array_Coins.length; j++) {
                        if (array_Coins[j] <= i  && (array_best[i] == 0 || (array_best[i - array_Coins[j]] + 1) <= array_best[i])) {
                            array_best[i] = array_best[i - array_Coins[j]] + 1;
                        }
                }
            }
            System.err.println("The Value is" + array_best[14]);
    
        }
    
    
        public static void main(String[] args) {
            int[] sequence1 = {11, 9,1, 3, 5,2 ,20};
            int sum = 30;
            calculateMinumCoins(sequence1, sum);
        }
    
    }
    

提交回复
热议问题