当前位置:网站首页>Complete knapsack problem to find the number of combinations and permutations
Complete knapsack problem to find the number of combinations and permutations
2022-08-01 16:32:00 【North Sea ghost fish not sleeping】

This is a typical application of the complete knapsack problem,Because it's just asking for numbers,It does not involve the case where the arrangement of the change is different and counted twice,所以两层for循环,外层遍历物品,内层遍历背包.
class Solution {
public int change(int amount, int[] coins) {
int[] dp = new int[amount+1];
dp[0] = 1;
for (int coin : coins) {
for (int i = coin;i<= amount;i++){
dp[i] += dp[i-coin];
}
}
return dp[amount];
}
}

The only difference between this one and the one above is the ordering,Different order counts twice,So here are two layersforThe traversal order of the loop is 外层遍历背包,内层遍历物品.
class Solution {
public int combinationSum4(int[] nums, int target) {
int[] dp = new int[target+1];
dp[0] = 1;
for(int j = 1;j<=target;j++){
for(int i = 0;i<nums.length;i++){
if(j>=nums[i]) dp[j] += dp[j-nums[i]];
}
}
return dp[target];
}
}
关于背包问题,参考文章.
边栏推荐
- 1个月写900多条用例,2线城市年薪33W+的测试经理能有多卷?
- Break the limit of file locks and use storage power to help enterprises grow new momentum
- 华盛顿大学、Allen AI 等联合 | RealTime QA: What's the Answer Right Now?(实时 QA:现在的答案是什么?)
- 直播app开发,是优化直播体验不得不关注的两大指标
- PAT 甲级 A1003 Emergency
- 暑气渐敛,8月让我们开源一夏!
- Use Canvas to implement mobile phone signature
- C#Excel帮助类
- TiFlash 存储层概览
- Synchronized原理
猜你喜欢

DOM series of touch screen events

IronOS, an open source system for portable soldering irons, supports a variety of portable DC, QC, PD powered soldering irons, and supports all standard functions of smart soldering irons

美国弗吉尼亚大学、微软 | Active Data Pattern Extraction Attacks on Generative Language Models(对生成语言模型的主动数据模式提取攻击)

DOM系列之触屏事件

Break the limit of file locks and use storage power to help enterprises grow new momentum

ECCV 2022 | Poseur:你以为我是姿态估计,其实是目标检测哒

指针进阶(三)之指针与数组笔试题
MySQL INTERVAL 关键字指南

提速!进口婴幼儿配方产品出证仅需1-3天

MUI as a mobile phone to return to the action bar
随机推荐
面对营销难,有米云指出一条破局之路
实习日报-2022-7-29
会议OA项目(六)--- (待开会议、历史会议、所有会议)
未来小间距竞争的着力点在哪里
Spark: Cluster Computing with Working Sets
工业制造行业的低代码开发平台思维架构图
便携烙铁开源系统IronOS,支持多款便携DC, QC, PD供电烙铁,支持所有智能烙铁标准功能
Go unit tests
PAT 甲级 A1030 Travel Plan
MUI as a mobile phone to return to the action bar
Zhaoqi Science and Technology Innovation Event Platform, Entrepreneurship Event Roadshow, Online Live Roadshow
选择合适的 DevOps 工具,从理解 DevOps 开始
聊下自己转型测试开发的历程
五分钟带你上手ShardingJDBC实现MySQL分库分表
mysql源码分析——聚簇索引
高薪程序员&面试题精讲系列131之Eureka如何实现高可用?自我保护机制是怎么回事?
DevExpress的GridControl帮助类
MySQL INTERVAL Keyword Guidelines
谁还敢买影视股?
PHP 安全漏洞:会话劫持、跨站点脚本、SQL 注入以及如何修复它们