当前位置:网站首页>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 + " ");
}
}
边栏推荐
- Ab3d.PowerToys and Ab3d.DXEngine Crack
- 手写分布式配置中心(1)
- [Cultivation of internal skills of memory operation functions] memcpy + memmove + memcmp + memset (4)
- The market value of 360 has evaporated by 390 billion in four years. Can government and enterprise security save lives?
- Uniapp dynamic sliding navigation effect demo (finishing)
- MySQL的安装与卸载
- TypeScript - the use of closure functions
- 【SSR服务端渲染+CSR客户端渲染+post请求+get请求+总结】
- Bidding Announcement | Operation and Maintenance Project of Haina Baichuang Official Account
- 对写作的一些感悟
猜你喜欢
随机推荐
Literature reading ten - Detect Rumors on Twitter by Promoting Information Campaigns with Generative Adversarial Learn
基于Appian低代码平台开发一个SpaceX网站
一点点读懂thermal(一)
uniapp horizontal tab (horizontal scrolling navigation bar) effect demo (organization)
[QNX Hypervisor 2.2用户手册]10.5 vdev ioapic
kernel hung_task死锁检测机制原理实现
MYS-6ULX-IOT 开发板测评——使用 Yocto 添加软件包
The Controller layer code is written like this, concise and elegant!
【七夕情人节特效】-- canvas实现满屏爱心
ClickHouse 二级索引
Pytest learning - fixtures
情人节---快来学习一下程序员的专属浪漫吧
使用OpenCV实现一个文档自动扫描仪
基于深度学习的路面坑洞检测(详细教程)
Develop a SpaceX website based on the Appian low-code platform
应用联合、体系化推进。集团型化工企业数字化转型路径
一点点读懂regulator(四)
DNS常见资源记录类型详解
建模师经验分享:模型学习方法
Ab3d.PowerToys and Ab3d.DXEngine Crack