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

KT6368A蓝牙的认证问题_FCC和BQB_CE_KC认证或者其它说明

【七夕情人节特效】-- canvas实现满屏爱心

SQL association table update

小黑leetcode冲浪:94. 二叉树的中序遍历

线程三连鞭之“线程的状态”
![[Cultivation of internal skills of string functions] strncpy + strncat + strncmp (2)](/img/9f/9221c081cfa86caccbbd02916a6208.png)
[Cultivation of internal skills of string functions] strncpy + strncat + strncmp (2)

功耗控制之DVFS介绍

Mathematical Principles of Matrix

kernel hung_task死锁检测机制原理实现

The market value of 360 has evaporated by 390 billion in four years. Can government and enterprise security save lives?
随机推荐
应用联合、体系化推进。集团型化工企业数字化转型路径
[Cultivation of internal skills of memory operation functions] memcpy + memmove + memcmp + memset (4)
jenkins发送邮件系统配置
The Go Programming Language (Introduction)
Ab3d.PowerToys and Ab3d.DXEngine Crack
如何根据地址获取函数名
MySQL的安装与卸载
头脑风暴:完全背包
Mathematical Principles of Matrix
【手撕AHB-APB Bridge】~ AMBA总线 之 AHB
ClickHouse 二级索引
Uniapp dynamic sliding navigation effect demo (finishing)
从单体架构迁移到 CQRS 后,我觉得 DDD 并不可怕
KT148A语音芯片怎么烧录语音进入芯片里面通过串口和电脑端的工具
MYS-6ULX-IOT 开发板测评——使用 Yocto 添加软件包
web3.js
深度|医疗行业勒索病毒防治解决方案
手写分布式配置中心(1)
一点点读懂cpufreq(二)
The role of the annotation @ EnableAutoConfiguration and how to use