当前位置:网站首页>Brainstorm: Complete Backpack
Brainstorm: Complete Backpack
2022-08-04 23:43:00 【InfoQ】
Title
Solution ideas
// Traverse the items first, thenTraverse backpack
for(int i = 0; i < weight.size(); i++) { // Traverse items
for(int j = weight[i]; j <= bagWeight ; j++) {// Traverse the knapsack capacity
dp[j] = max(dp[j], dp[j - weight[i]] + value[i]);
}
}
Code Implementation
private static void testCompletePack(){
int[] weight = {1, 3, 4};
int[] value = {15, 20, 30};
int bagWeight = 4;
int[] dp = new int[bagWeight + 1];
for (int i = 0; i< weight.length; i++){ // Traverse items
for (int j = weight[i]; j <= bagWeight; j++){ // Traverse bag capacity
dp[j] = Math.max(dp[j], dp[j - weight[i]] + value[i]);
}
}
for (int maxValue : dp){
System.out.println(maxValue + " ");
}
}
边栏推荐
- 未上市就“一举成名”,空间媲美途昂,安全、舒适一个不落
- 2022年华数杯数学建模
- 如何写好测试用例
- 堪称奔驰“理财产品”,空间媲美宝马X5,采用了非常运动的外观
- @Async注解的作用以及如何实现异步监听机制
- 基于Appian低代码平台开发一个SpaceX网站
- npm基本操作及命令详解
- [QNX Hypervisor 2.2用户手册]10.5 vdev ioapic
- The market value of 360 has evaporated by 390 billion in four years. Can government and enterprise security save lives?
- 365天深度学习训练营-学习线路
猜你喜欢
Day118.尚医通:订单列表、详情、支付
【CVA估值训练营】财务建模指南——第一讲
MySQL基础篇【聚合函数】
未上市就“一举成名”,空间媲美途昂,安全、舒适一个不落
招标公告 | 海纳百创公众号运维项目
入门3D游戏建模师知识必备
How to burn the KT148A voice chip into the chip through the serial port and the tools on the computer
MongoDB权限验证开启与mongoose数据库配置
The market value of 360 has evaporated by 390 billion in four years. Can government and enterprise security save lives?
Will we still need browsers in the future?(feat. Maple words Maple language)
随机推荐
2022/8/3
应用联合、体系化推进。集团型化工企业数字化转型路径
情人节---快来学习一下程序员的专属浪漫吧
小黑leetcode之旅:95. 至少有 K 个重复字符的最长子串
从单体架构迁移到 CQRS 后,我觉得 DDD 并不可怕
基于Appian低代码平台开发一个SpaceX网站
未上市就“一举成名”,空间媲美途昂,安全、舒适一个不落
SQL关联表更新
一点点读懂Thremal(二)
招标公告 | 海纳百创公众号运维项目
mysql基础
How to burn the KT148A voice chip into the chip through the serial port and the tools on the computer
PID Controller Improvement Notes No. 7: Improve the anti-overshoot setting of the PID controller
[QNX Hypervisor 2.2用户手册]10.6 vdev mc146818
Linear DP (bottom)
The Controller layer code is written like this, concise and elegant!
LeetCode Hot 100
First, the basic concept of reptiles
Basic web in PLSQL
学会反射后,我被录取了(干货)