当前位置:网站首页>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. 零钱兑换
iNFTnews | 迪士尼如何布局Web3
2022-08-09 mysql/stonedb-慢SQL-Q16分析
请讲一讲JS中的 for...in 与 for...of (上)
What are the basic steps to develop a quantitative trading strategy?
YGG 经理人杯总决赛已圆满结束,来看看这份文字版总结!
Interfering with BGP routing---community attributes
浅析量股票化交易的发展现状
Janus Official DEMO Introduction
Leetcode 701. 二叉搜索树中的插入操作
深度学习100例 —— 循环神经网络(RNN)实现股票预测
金仓数据库 KingbaseGIS 使用手册(6.6. 几何对象校验函数、6.7. 空间参考系函数)
【TS技术课堂】时间序列预测
&&、||、&、|
JS--hashchange事件--使用/教程
1018.值周
你的手机曾经被监控过吗?
异常处理(try,catch,finally)
力扣:474.一和零
中国SaaS企业排名,龙头企业Top10梳理









