当前位置:网站首页>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 + " ");
}
}
边栏推荐
- The role of the annotation @ EnableAutoConfiguration and how to use
- 【无标题】
- C语言实现扫雷 附带源代码
- I was rejected by the leader for a salary increase, and my anger rose by 9.5K after switching jobs. This is my mental journey
- 2022年华数杯数学建模
- what is MVCC
- MySQL基础篇【聚合函数】
- Statistical words (DAY 101) Huazhong University of Science and Technology postgraduate examination questions
- uniapp horizontal tab (horizontal scrolling navigation bar) effect demo (organization)
- uniapp横向选项卡(水平滚动导航栏)效果demo(整理)
猜你喜欢

基于深度学习的路面坑洞检测(详细教程)
![[Happy Qixi Festival] How does Nacos realize the service registration function?](/img/df/5793145da45bc80d227b0babfac914.png)
[Happy Qixi Festival] How does Nacos realize the service registration function?

Nuclei (2) Advanced - In-depth understanding of workflows, Matchers and Extractors

Go 语言快速入门指南:什么是 TSL 安全传输层

truffle

直接插入排序

为何越来越多人选择进入软件测试行业?深度剖析软件测试的优势...

Uniapp dynamic sliding navigation effect demo (finishing)

应用联合、体系化推进。集团型化工企业数字化转型路径

【无标题】线程三连鞭之“线程池”
随机推荐
零基础如何入门软件测试?再到测开(小编心得)
KT148A语音芯片ic工作原理以及芯片的内部架构描述
注解@EnableAutoConfiguration的作用以及如何使用
3. Actual combat---crawl the result page corresponding to Baidu's specified entry (a simple page collector)
一点点读懂cpufreq(二)
MongoDB权限验证开启与mongoose数据库配置
Cython
MySQL的安装与卸载
小黑leetcode之旅:95. 至少有 K 个重复字符的最长子串
情侣牵手[贪心 & 抽象]
Basic web in PLSQL
一点点读懂Thremal(二)
深度|医疗行业勒索病毒防治解决方案
2022年华数杯数学建模
d枚举生成位
Nuclei (2) Advanced - In-depth understanding of workflows, Matchers and Extractors
【软件测试】常用ADB命令
Community Sharing|Tencent Overseas Games builds game security operation capabilities based on JumpServer
【SSR服务端渲染+CSR客户端渲染+post请求+get请求+总结】
再肝3天,整理了90个 NumPy 例子,不能不收藏!