当前位置:网站首页>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 + " ");
}
}
边栏推荐
猜你喜欢

Security software Avast and Symantec NortonLifeLock merge with UK approval, market value soars 43%

The market value of 360 has evaporated by 390 billion in four years. Can government and enterprise security save lives?

Day118. Shangyitong: order list, details, payment

隐私计算综述

NebulaGraph v3.2.0 Release Note, many optimizations such as the performance of querying the shortest path

基于深度学习的路面坑洞检测(详细教程)

线程三连鞭之“线程的状态”

三、实战---爬取百度指定词条所对应的结果页面(一个简单的页面采集器)

First, the basic concept of reptiles

建模师经验分享:模型学习方法
随机推荐
对“为什么一些程序员很傲慢”的解读
kernel hung_task死锁检测机制原理实现
ClickHouse 二级索引
Security software Avast and Symantec NortonLifeLock merge with UK approval, market value soars 43%
typeScript-promise
Literature reading ten - Detect Rumors on Twitter by Promoting Information Campaigns with Generative Adversarial Learn
加解密在线工具和进制转化在线工具
一点点读懂Thremal(二)
npm基本操作及命令详解
NebulaGraph v3.2.0 Release Note,对查询最短路径的性能等多处优化
再肝3天,整理了90个 NumPy 例子,不能不收藏!
OPENCV学习DAY8
测试技术:关于上下文驱动测试的总结
吐槽 | 参加IT培训的正确姿势
【无标题】线程三连鞭之“线程池”
How to burn the KT148A voice chip into the chip through the serial port and the tools on the computer
[Happy Qixi Festival] How does Nacos realize the service registration function?
七牛云图片上传
Pytorch分布式训练/多卡/多GPU训练DDP的torch.distributed.launch和torchrun
web3.js