当前位置:网站首页>动态规划:硬币题目总结
动态规划:硬币题目总结
2022-04-21 17:38:00 【yilyil】
-
arr货币数组,其中值都是整数。再给定一个整数aim,每个值都被认为是一张货币,即便值相同的货币也认为每一张都是不同的,返回amin的方法数
-
arr货币数组,其中值都是不重复整数。再给定一个整数aim,每个货币的张数是无限的,返回amin的方法数
-
arr货币数组,其中值可以是是重复整数。再给定一个整数aim,每个相同值都被认为是同一种货币,返回amin的方法数
-
arr货币数组,其中值都是不重复整数。再给定一个整数aim,每个货币的张数是无限的,返回构成aim的最少张数
从尝试模型来看

从尝试代码来看(尝试模型导致)
public static int process(int[] arr, int index, int rest) {
if (rest < 0) {
return 0;
}
if (index == arr.length) {
// 没钱了!
return rest == 0 ? 1 : 0;
}
return process(arr,index+1,rest)+process(arr,index+1,rest-arr[index]);
}
public static int process(int[] arr, int index, int rest) {
if (index == arr.length) {
return rest == 0 ? 1 : 0;
}
int ways = 0;
for (int zhang = 0; zhang * arr[index] <= rest; zhang++) {
ways += process(arr, index + 1, rest - (zhang * arr[index]));
}
return ways;
}
public static int process(int[] coins, int[] num, int index, int rest) {
if (index == coins.length) {
return rest == 0 ? 1 : 0;
}
int ways = 0;
for (int zhang = 0; zhang * coins[index] <= rest && zhang <= num[index]; zhang++) {
ways += process(coins, num, index + 1, rest - (zhang * coins[index]));
}
return ways;
}
public static int process(int[] arr, int index, int rest) {
if (index == arr.length) {
return rest == 0 ? 0 : Integer.MAX_VALUE;
} else {
int ans = Integer.MAX_VALUE;
for (int zhang = 0; zhang * arr[index] <= rest; zhang++) {
int next = process(arr, index + 1, rest - zhang * arr[index]);
if (next != Integer.MAX_VALUE) {
ans = Math.min(ans, zhang + next);
}
}
return ans;
}
}
从dp表看,尝试模型代码

版权声明
本文为[yilyil]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_42146775/article/details/124158721
边栏推荐
猜你喜欢

Plug - in de date de téléphone mobile (ajouter ce que vous aimez)

胸部X光图像-数据集

Fixturlaser对中仪维修GO/NXA Pro/ECO/EVO系列

我用Ehcache把查询性能提升了100倍,真香
MySQL ODBC驱动简介

洞见科技首批通过央行国家金融科技测评中心「联邦学习」产品评测,实现「MPC+FL」金融应用双认证

老电脑应该怎么重装系统比较好

pytorch index_add_用法介绍

Image Manipulation Detection by Multi-View Multi-Scale Supervision
理想ONE终于换代?全新产品序列定名L8,重度伪装路试照曝光
随机推荐
pytorch index_ add_ Usage introduction
Redis三种模式——主从复制,哨兵模式,集群
Advantages of MySQL
前五章内容思维导图
[machine learning] gating cycle unit
A simple and easy-to-use file upload scheme
如何设置Win11账户密码有效期?Win11账户密码使用期限设置教程
WPF 引用 UWP 控件 不打包为 MSIX 分发的方法
Fault analysis | federated storage engine table causes the monitoring thread to be in the opening table state
About the internal supposition
游戏合作伙伴专题:BreederDAO 与 Crypto Unicorns 建立合作伙伴关系
第118章 SQL函数 REVERSE
MOS管和IGBT有什么区别?别傻傻分不清了
STM32 learning notes - sub second value calibration of RTC
JVM 从入门到放弃之 ZGC 垃圾收集器
MySQL进阶之常用函数
诚邀报名丨首期OpenHarmony开发者成长计划分享日
【ACM/webank】#491.递增子序列(使用HashSet来记录并防止重复子序列)
Understand prototype patterns in minutes
Detection, tracking, behavior recognition all in one! Industrial pedestrian analysis system is heavy and open source!