当前位置:网站首页>Change exchange (dynamic planning)
Change exchange (dynamic planning)
2022-06-28 15:54:00 【hide17】
Give you an array of integers coins , Coins of different denominations ; There is no limit to the amount of each coin ; And an integer amount , Represents the total amount , Ask how many coins you need at least to round up the amount , If no combination of coins can make up the total amount , return -1 .
for example N = 3, The denominations are 1,2,5, Total sum amount = 11. So at least 3 A coin makes up , namely 11 = 5 + 5 + 1.
First , This problem is a dynamic programming problem , Because it has 「 Optimal substructure 」 Of . Must conform to 「 Optimal substructure 」, The subproblems must be independent of each other .
Why is it consistent with the optimal substructure ? Assuming a face value of 1, 2, 5 The coin of , seek amount = 11 Minimum number of coins per hour ( The original question ), If you know how to come up with amount = 10, 9, 6 The minimum number of coins ( Sub problem ), Just add one to the answer to the Subquestion ( Choose another one with a face value of 1, 2, 5 The coin of ), Find a minimum value , It's the answer to the original question . Because there is no limit to the number of coins , So there is no interaction between subproblems , It's independent of each other .
Then according to the dynamic programming problem , List the correct state transition equations :
1.base case (amount by 0 when , The algorithm returns 0)
2.state ( Variables that will change in the original problem and subproblem ) The number of coins is unlimited , The denomination of the coin is also given by the title , Only the target amount will continue to base case near , So the only one 「 state 」 Is the target amount amount
3.choice ( Cause the state to change ) Every coin you choose , It's equivalent to reducing the target amount
4.dp ( function / Definition of array ) A recursive dp function , The parameter of a function is the quantity that will change in the state transition
dp(n) Express , Enter a target amount n, Return the rounded up target amount n The minimum number of coins required .
Solution pseudocode
int coinChange(int[]coins,int amount){
return dp(coins,amount)
}// Definition : To make up the amount n, At the very least dp(coins, n) A coin
int dp(int[] coins, int n) {
// Choose the result that needs the least coins
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]; } }
边栏推荐
- VC2010 编绎Qt5.6.3 提示 CVTRES : fatal error CVT1107:
- 首次失败后,爱美客第二次冲刺港交所上市,财务负责人变动频繁
- 不要使用短路逻辑编写 stl sorter 多条件比较
- Summary of language features of fluent dart
- 机器学习之深度学习卷积神经网络,实现基于CNN网络的手写字体识别
- Flutter简单实现多语言国际化
- 国债与定期存款哪个更安全 两者之间有何区别
- ROS knowledge points - definition and use of topic messages
- ROS知识点——ROS创建工作空间
- Privacy computing fat - offline prediction
猜你喜欢

Practice of curve replacing CEPH in Netease cloud music

Coding Devops helps Sinochem information to build a new generation of research efficiency platform and drive the new future of "online Sinochem"

Classic model transformer

Analysis of PostgreSQL storage structure

Qt5.5.1 configuring msvc2010 compiler and WinDbg debugger

C语言学习-20-归并排序

Fleet | "backstage exploration" issue 3: status management

隆重推出 Qodana:您最爱的 CI 的代码质量平台
PostgreSQL enables grouping statistics by year, month, day, week, hour, minute and second

Xinchuang operating system -- kylin kylin desktop operating system (project 10 security center)
随机推荐
部门新来了个字节25K出来的,让我见识到了什么是天花板
使用openpyxl操作Excel
首次失败后,爱美客第二次冲刺港交所上市,财务负责人变动频繁
10:00面试,10:02就出来了 ,问的实在是太...
有哪些好用的供应商管理系统
VS2013 帮助文档中没有 win32/com
关注35岁的坎:畏惧是因为你没有匹配这个年纪该有的能力
Visual Studio 2010 compilation qt5.6.3
看界面控件DevExpress WinForms如何创建一个虚拟键盘
软件测试员的悲哀竟是...自己的技术能力不能满足大厂要求?
机器学习之深度学习卷积神经网络,实现基于CNN网络的手写字体识别
Navicat 15 for MySQL
10年测试经验,在35岁的生理年龄面前,一文不值
No win32/com in vs2013 help document
MIPS汇编语言学习-01-两数求和以及环境配置、如何运行
Validate palindrome string
零钱兑换(动态规划)
MIPS assembly language learning-03-cycle
FFmpeg之禁止输出banner log(三十)
分布式理论须知