当前位置:网站首页>零钱兑换(动态规划)
零钱兑换(动态规划)
2022-06-28 15:38:00 【hide17】
给你一个整数数组 coins ,表示不同面额的硬币;每种硬币的数量无限;以及一个整数 amount ,表示总金额,问最少需要几枚硬币凑出这个金额,如果没有任何一种硬币组合能组成总金额,返回 -1 。
例如 N = 3,面值分别为 1,2,5,总金额 amount = 11。那么最少需要 3 枚硬币凑出,即 11 = 5 + 5 + 1。
首先,这个问题是动态规划问题,因为它具有「最优子结构」的。要符合「最优子结构」,子问题间必须互相独立。
为什么符合最优子结构? 假设有面值为 1, 2, 5 的硬币,求 amount = 11 时的最少硬币数(原问题),如果你知道凑出 amount = 10, 9, 6 的最少硬币数(子问题),只需要把子问题的答案加一(再选一枚面值为 1, 2, 5 的硬币),求个最小值,就是原问题的答案。因为硬币的数量是没有限制的,所以子问题之间没有相互制,是互相独立的。
然后根据动态规划问题, 列出正确的状态转移方程:
1.base case (amount为0时,算法返回0)
2.state (原问题和子问题中会变化的变量) 硬币数量无限,硬币的面额也是题目给定的,只有目标金额会不断地向 base case 靠近,所以唯一的「状态」就是目标金额 amount
3.choice (导致状态产生变化 )每选择一枚硬币,就相当于减少了目标金额
4.dp (函数/数组的定义 ) 一个递归的 dp 函数, 函数的参数就是状态转移中会变化的量
dp(n) 表示,输入一个目标金额 n,返回凑出目标金额 n 所需的最少硬币数量。
解法伪代码
int coinChange(int[]coins,int amount){
return dp(coins,amount)
}// 定义:要凑出金额 n,至少要 dp(coins, n) 个硬币
int dp(int[] coins, int n) {
// 选择需要硬币最少的那个结果
for (int coin : coins) {
result = min(res, 1 + dp(n - coin))
}
return result
}class Solution { public int coinChange(int[] coins, int amount) { if(amount == 0) return 0; if(amount<=0) return -1; int[] dp = new int[amount+1]; Arrays.fill(dp,amount+1); dp[0] = 0; for(int coin:coins){ for(int i = coin; i <= amount; i++){ dp[i] = Math.min(dp[i],dp[i-coin]+1); } } return dp[amount]==amount+1?-1:dp[amount]; } }
边栏推荐
- What useful supplier management systems are available
- QT create 5.0.3 configuring qt4.8.7
- Go zero micro Service Practice Series (VII. How to optimize such a high demand)
- SAP MTS/ATO/MTO/ETO专题之九:M+M模式前后台操作,策略用50,提前准备原材料和半成品
- In depth learning foundation summary
- DBMS in Oracle_ output. put_ Line output problem solving process
- Visual Studio 2010 configuring and using qt5.6.3
- The best time to buy and sell stocks
- 看界面控件DevExpress WinForms如何创建一个虚拟键盘
- 【MySQL】官网文档学习之查询语句sql注意事项
猜你喜欢

教育行业SaaS应用管理平台解决方案:助力企业实现经营、管理一体化

SAP MTS/ATO/MTO/ETO专题之九:M+M模式前后台操作,策略用50,提前准备原材料和半成品

SaaS application management platform solution in the education industry: help enterprises realize the integration of operation and management

经典模型——Transformer

Express template engine

Not being a meta universe now is like not buying a house 20 years ago!

字节跳动数据平台技术揭秘:基于 ClickHouse 的复杂查询实现与优化

Realization of a springboard machine

GCC efficient graph revolution for joint node representationlearning and clustering

S2b2c system website solution for kitchen and bathroom electrical appliance industry: create s2b2c platform Omni channel commercial system
随机推荐
Fleet | "backstage exploration" issue 3: status management
Installation and use of Jenkins
石油化工行业供应链系统驱动管理模式创新升级,强化企业内部管理
VC2010 编绎Qt5.6.3 提示 CVTRES : fatal error CVT1107:
【LeetCode】13、罗马数字转整数
大神详解开源 BUFF 增益攻略丨直播讲座
Oracle11g database uses expdp to back up data every week and upload it to the backup server
REDIS00_详解redis.conf配置文件
Fleet |「后台探秘」第 3 期:状态管理
wallys/DR7915-wifi6-MT7915-MT7975-2T2R-support-OpenWRT-802.11AX-supporting-MiniPCIe-Module
【MySQL】表连接为什么比子查询快
Privacy computing fat - offline prediction
Application of mongodb in Tencent retail premium code
Web worker poll request
ROS知识点——ROS创建工作空间
Fleet | background Discovery issue 3: Status Management
go-zero 微服务实战系列(七、请求量这么高该如何优化)
openGauss内核:SQL解析过程分析
Xinchuang operating system -- kylin kylin desktop operating system (project 10 security center)
Opengauss kernel: analysis of SQL parsing process