当前位置:网站首页>零钱兑换(动态规划)
零钱兑换(动态规划)
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 is the difference between treasury bonds and time deposits
- 教育行业SaaS应用管理平台解决方案:助力企业实现经营、管理一体化
- Sample explanation of batch inserting data using MySQL bulkloader
- MIPS assembly language learning -02- logic judgment - foreground input
- 深度学习基础汇总
- Basic grammar of C language
- S2b2c system website solution for kitchen and bathroom electrical appliance industry: create s2b2c platform Omni channel commercial system
- 征文投稿丨使用轻量应用服务器搭建博客环境
- SaaS application management platform solution in the education industry: help enterprises realize the integration of operation and management
- web Worker 轮询请求
猜你喜欢

Basic grammar of C language

Flutter简单实现多语言国际化

Naacl 2022 | distillation of machinetranslation SOTA model

Halcon basic summary (I) cutting pictures and rotating images
![[leetcode] 13. Roman numeral to integer](/img/3c/7c57d0c407f5302115f69f44b473c5.png)
[leetcode] 13. Roman numeral to integer

Realization of a springboard machine

Application of mongodb in Tencent retail premium code

MIPS assembly language learning-01-sum of two numbers, environment configuration and how to run

【推荐系统】多任务学习之ESMM模型(更新ing)

【LeetCode】13、罗马数字转整数
随机推荐
[leetcode] 13. Roman numeral to integer
Opengauss kernel: analysis of SQL parsing process
MIPS assembly language learning-01-sum of two numbers, environment configuration and how to run
Naacl 2022 | distillation of machinetranslation SOTA model
Spark SQL generate JSON
扩充C盘(将D盘的内存分给C盘)
一种跳板机的实现思路
教育行业SaaS应用管理平台解决方案:助力企业实现经营、管理一体化
Fleet |「後臺探秘」第 3 期:狀態管理
分布式理论须知
Vc2010 compilation qt5.6.3 prompt cvtres: fatal error cvt1107:
机器学习之深度学习卷积神经网络,实现基于CNN网络的手写字体识别
Visual Studio 2010 configuring and using qt5.6.3
Expand Disk C (allocate the memory of disk d to Disk C)
Visual Studio 2010 compilation qt5.6.3
ROS knowledge points - build an ROS development environment using vscode
10:00面试,10:02就出来了 ,问的实在是太...
Privacy computing fat - offline prediction
sql语句 练习题
See how the interface control devaxpress WinForms creates a virtual keyboard