当前位置:网站首页>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];}};
边栏推荐
猜你喜欢
随机推荐
多线程是同时执行多个线程的吗
力扣:322. 零钱兑换
Leetcode 98. 验证二叉搜索树
2020年度SaaS TOP100企业名单
JS中表单操作、addEventListener事件监听器
守护进程
用PLSQL导出Oracle一个表
上海一科技公司刷单被罚22万,揭露网络刷单灰色产业链
毕昇编译器优化:Lazy Code Motion
深圳堡垒机厂家有哪些?重点推荐哪家?
2022-08-09 mysql/stonedb-慢SQL-Q16分析
Filament-Material 绘制基本图形
友元类和友元函数
mysql中的key是怎么用的,或者这个值有什么意义,如下图?
CV复习:softmax代码实现
Chapter 15 HMM模型
函数习题(下)
UNI-APP_ monitor page scroll h5 monitor page scroll
高数_复习_第4章:向量代数和空间解析几何
kubesphere