当前位置:网站首页>Click: 518. Change Exchange II
Click: 518. Change Exchange II
2022-08-10 00:32:00 【empty__barrel】
Likou: 518. Coin Change II
Title:
Give you an integer array coins representing coins of different denominations, and another integer amount representing the total amount.
Please calculate and return the number of coin combinations that can make up the total amount.Returns 0 if none of the coin combinations make up the total.
Suppose there are infinite coins of each denomination.
The title data guarantees that the result conforms to a 32-bit signed integer.
dp array meaning:
dp[j]: The number of currency combinations that make up the total amount j is dp[j]
Recursive formula:
dp[j] += dp[j - coins[i]];
Initialization:
- First of all, dp[0] = 1 means dp[i] means that the number of currency combinations that make up the total amount of 0 is 1.
- The dp[j] whose subscript is not 0 is initialized to 0, so that the cumulative addition of dp[j - coins[i]] will not affect the real dp[j]
Traversal order:
The complete knapsack problem is also a combination, so it is traversed from small to large, first for items and then nested for knapsack capacity.
Code:
class Solution {public:int change(int amount, vector<int>& coins) {vector<int> dp(amount+1,0);dp[0] = 1;int bagweight = amount;for(int i = 0; i <coins.size(); ++i){for(int j = coins[i]; j <= bagweight; ++j){dp[j] += dp[j-coins[i]];}}return dp[amount];}}; 边栏推荐
猜你喜欢

Sun Zhengyi lost 150 billion: it was expensive at the beginning

全面解析FPGA基础知识

Vmware中安装win7虚拟机以及相关简单知识

shader学习笔记(五)

OSG笔记:使用setFontResolution设置字体分辨率

torch.distributed多卡/多GPU/分布式DPP(二)——torch.distributed.all_reduce(reduce_mean)&barrier&控制进程执行顺序&随机数种子

CV复习:softmax代码实现

【对象——对象及原型链上的属性——对象的操作方法】

华为云全流程护航《流浪方舟》破竹首发,打造口碑爆款

VR全景结合小程序,为线上电商更好的服务
随机推荐
Leetcode 701. 二叉搜索树中的插入操作
Analyses the development status quo of stock trading
33. Fabric通道、组织、节点、权限间关系
Controller层代码这么写,简洁又优雅!
OSG笔记:使用setFontResolution设置字体分辨率
pip 离线到内网安装包
Leetcode 236. 二叉树的最近公共祖先
打包报错 AAPT: error: failed to read PNG signature: file does not start with PNG signature.
伦敦银行情中短线的支撑和阻力位
DXF笔记:文字对齐的研究
干涉BGP的选路---社团属性
函数习题(下)
使用股票量化交易接口需要具备怎么样的心态
探索TiDB Lightning源码来解决发现的bug
Gartner全球集成系统市场数据追踪,超融合市场增速第一
金仓数据库 KingbaseGIS 使用手册(6.2. 管理函数)
信息系统项目管理师---第十一章项目风险管理历年考题
请讲一讲JS中的 for...in 与 for...of (上)
金仓数据库 KingbaseGIS 使用手册(6.3. 几何对象创建函数)
力扣:322. 零钱兑换