当前位置:网站首页>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 + " ");
}
}
边栏推荐
- uniapp 分享功能-分享给朋友群聊朋友圈效果(整理)
- 【七夕情人节特效】-- canvas实现满屏爱心
- The role of @Async annotation and how to implement asynchronous listening mechanism
- 再肝3天,整理了90个 NumPy 例子,不能不收藏!
- The market value of 360 has evaporated by 390 billion in four years. Can government and enterprise security save lives?
- ~ hand AHB - APB Bridge 】 【 AMBA AHB bus
- After another 3 days, I have sorted out 90 NumPy examples, and I can't help but bookmark it!
- truffle
- 安全软件 Avast 与赛门铁克诺顿 NortonLifeLock 合并案获英国批准,市值暴涨 43%
- 2022牛客暑期多校训练营5(BCDFGHK)
猜你喜欢

【CVA估值训练营】财务建模指南——第一讲

Pytest learning - fixtures

一点点读懂cpufreq(一)

Basic web in PLSQL

学会反射后,我被录取了(干货)

C5750X7R2E105K230KA(电容器)MSP430F5249IRGCR微控制器资料

Day118. Shangyitong: order list, details, payment

OpenCV:10特征检测

Statistical words (DAY 101) Huazhong University of Science and Technology postgraduate examination questions

一点点读懂cpufreq(二)
随机推荐
3年,从3K涨薪到20k?真是麻雀啄了牛屁股 — 雀食牛逼呀
对写作的一些感悟
对“为什么一些程序员很傲慢”的解读
Xiaohei's leetcode journey: 95. Longest substring with at least K repeating characters
Basic web in PLSQL
[QNX Hypervisor 2.2用户手册]10.4 vdev hpet
Literature reading ten - Detect Rumors on Twitter by Promoting Information Campaigns with Generative Adversarial Learn
Mathematical Principles of Matrix
MySQL基础篇【聚合函数】
2022/8/4 树上差分+线段树
Xiaohei leetcode surfing: 94. Inorder traversal of binary tree
[Happy Qixi Festival] How does Nacos realize the service registration function?
C5750X7R2E105K230KA(电容器)MSP430F5249IRGCR微控制器资料
MySQL的安装与卸载
应用联合、体系化推进。集团型化工企业数字化转型路径
VMware NSX 4.0 -- 网络安全虚拟化平台
招标公告 | 海纳百创公众号运维项目
未上市就“一举成名”,空间媲美途昂,安全、舒适一个不落
KT148A电子语音芯片ic方案适用的场景以及常见产品类型
怎么将自己新文章自动推送给自己的粉丝(巨简单,学不会来打我)