【力扣】零钱兑换和零钱兑换2,动态规划算法

【力扣】零钱兑换和零钱兑换2,动态规划算法

最大组合数和最小物品数

package day0325;

/**
 * @description 动态规划
 * @date 2024/3/25
 */
public class Demo2 {

    public static void main(String[] args) {
        // 3
//        int[] coins = {1, 2, 5};
//        int amount = 11;

        // 5
//        int[] coins = {1, 6, 7};
//        int amount = 30;

        // 20
        int[] coins = {186, 419, 83, 408};
        int amount = 6249;

        int count = getMinCoinCount(amount, coins);
        System.out.println(count);


        // 4
//        int[] coins = {1, 2, 5};
//        int amount = 5;

        // 0
//        int[] coins = {2};
//        int amount = 3;

        // 1
//        int[] coins = {10};
//        int amount = 10;

//        int count = getMaxCombineCount(amount, coins);
//        System.out.println(count);
    }

    /**
     * <p>最大组合数</p>
     *
     * @param amount 总金额(背包容量)
     * @param coins  零钱种类(物品种类)
     * @return
     */
    private static int getMaxCombineCount(int amount, int[] coins) {
        int[] dp = new int[amount + 1];
        dp[0] = 1;
        for (int i = 0; i < coins.length; i++) {
            for (int j = coins[i]; j < dp.length; j++) {
                dp[j] = dp[j] + dp[j - coins[i]];
            }
        }
        return dp[amount];
    }

    /**
     * <p>最小物品数</p>
     *
     * @param amount 总金额(背包容量)
     * @param coins  零钱种类(物品种类)
     * @return
     */
    private static int getMinCoinCount(int amount, int[] coins) {
        int[] dp = new int[amount + 1];
        for (int i = 0; i < dp.length; i++) {
            dp[i] = amount + 1;
        }
        dp[0] = 0;
        for (int coin : coins) {
            for (int j = coin; j < dp.length; j++) {
                dp[j] = Math.min(dp[j], dp[j - coin] + 1);
            }
        }
        return dp[amount] > amount ? -1 : dp[amount];
    }

}

打印最少数量组合

package day0325;

import java.util.*;
import java.util.stream.IntStream;

/**
 * @description 最少数量组合 记录是哪种组合
 * @date 2024/3/25
 */
public class Demo1 {

    public static void main(String[] args) {
        // 3
        // 2*5   1*1
        // 11
        // int[] coins = {1, 2, 5};
        //int amount = 11;

        // [186, 186, 186, 419, 419, 419, 419, 419, 83, 83, 83, 83, 408, 408, 408, 408, 408, 408, 408, 408]
        int[] coins = {186, 419, 83, 408};
        int amount = 6249;

        // 20
        // 3*186   5*419   4*83   8*408
        // 558   2095   332   3264
        // 6249
        System.out.println(186 + 186 + 186 + 419 + 419 + 419 + 419 + 419 + 83 + 83 + 83 + 83 + 408 + 408 + 408 + 408 + 408 + 408 + 408 + 408);

        // 5
        // 5*6
        // 30
        // int[] coins = {1, 6, 7};
        // int amount = 30;

        // 记录组合
        Map<Integer, List<Integer>> amountListMap = IntStream.rangeClosed(0, amount).collect(HashMap::new, (map, i) -> map.put(i, new ArrayList<>()), HashMap::putAll);

        int[] dp = new int[amount + 1];
        Arrays.fill(dp, amount + 1);
        dp[0] = 0;
        for (int i = 0; i < coins.length; i++) {
            for (int j = 1; j < dp.length; j++) {
                if (j >= coins[i]) {
                    int count = dp[j - coins[i]];
                    int newCount = Math.min(dp[j], count + 1);
                    if (newCount == count + 1) {
                        List<Integer> currentList = new ArrayList<>();
                        if (count == 0) {
                            currentList.add(coins[i]);
                            amountListMap.put(j, currentList);
                        } else {
                            List<Integer> otherList = amountListMap.get(j - coins[i]);
                            currentList.addAll(otherList);
                            currentList.add(coins[i]);
                            amountListMap.put(j, currentList);
                        }
                    }
                    dp[j] = newCount;
                }
            }
        }

//        System.out.println(amountListMap);
        System.out.println(amountListMap.get(amount));

        System.out.println("-----------------------------------------------------------------------------------------------");

//        System.out.println(Arrays.toString(dp));
        System.out.println(dp[amount] > amount ? -1 : dp[amount]);
    }

}

打印所有的组合

package day0325;

import java.util.*;
import java.util.stream.IntStream;

/**
 * @description 所有的组合
 * @date 2024/3/25
 */
public class Demo3 {

    public static void main(String[] args) {
        // 4
        // 1+1+1+1+1   1+1+1+2   1+2+2   5
        int[] coins = {1, 2, 5};
        int amount = 5;

        Map<Integer, List<List<Integer>>> everyAmountCombineListMap = IntStream.rangeClosed(0, 5).collect(HashMap::new, (map, i) -> map.put(i, new ArrayList<>()), HashMap::putAll);

        int[] dp = new int[amount + 1];
        dp[0] = 1;
        List<List<Integer>> zeroList = new ArrayList<>();
        List<Integer> innerZeroList = new ArrayList<>();
        innerZeroList.add(0);
        zeroList.add(innerZeroList);
        everyAmountCombineListMap.put(0, zeroList);

        for (int i = 0; i < coins.length; i++) {
            for (int j = coins[i]; j < dp.length; j++) {
                List<List<Integer>> amountCombineList = new ArrayList<>();
                if (j - coins[i] == 0) {
                    List<Integer> innerList = new ArrayList<>();
                    innerList.add(j);
                    amountCombineList.add(innerList);
                } else {
                    int otherIndex = j - coins[i];
                    List<List<Integer>> otherList = everyAmountCombineListMap.get(otherIndex);
                    for (List<Integer> l : otherList) {
                        // 必须创建新的容器,不能破坏之前的数据
                        List<Integer> currentList = new ArrayList<>(l);
                        currentList.add(coins[i]);
                        amountCombineList.add(currentList);
                    }
                }
                if (dp[j] != 0) {
                    List<List<Integer>> otherList = everyAmountCombineListMap.get(j);
                    amountCombineList.addAll(otherList);
                }
                everyAmountCombineListMap.put(j, amountCombineList);

                dp[j] = dp[j] + dp[j - coins[i]];
            }
        }

        // [[5], [1, 2, 2], [1, 1, 1, 2], [1, 1, 1, 1, 1]]
        System.out.println(everyAmountCombineListMap);
        System.out.println(everyAmountCombineListMap.get(amount));

        System.out.println("--------------------------------------------------------------------------------------------------------------------");

        System.out.println(everyAmountCombineListMap.get(amount).size());
        System.out.println(dp[amount]);
    }

}

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值