当前位置:网站首页>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 + " ");
}
}
边栏推荐
- 加解密在线工具和进制转化在线工具
- kernel问题定位手段总结
- 什么是次世代建模(附学习资料)
- Basic web in PLSQL
- uniapp横向选项卡(水平滚动导航栏)效果demo(整理)
- 一点点读懂cpufreq(一)
- MySQL的安装与卸载
- 3. Actual combat---crawl the result page corresponding to Baidu's specified entry (a simple page collector)
- Flutter启动流程(Skia引擎)介绍与使用
- The role of @Async annotation and how to implement asynchronous listening mechanism
猜你喜欢

Will we still need browsers in the future?(feat. Maple words Maple language)

安全软件 Avast 与赛门铁克诺顿 NortonLifeLock 合并案获英国批准,市值暴涨 43%

jenkins发送邮件系统配置

KT6368A Bluetooth certification problem_FCC and BQB_CE_KC certification or other instructions

【软件测试】常用ADB命令

堪称奔驰“理财产品”,空间媲美宝马X5,采用了非常运动的外观

407. 接雨水 II

Ab3d.PowerToys and Ab3d.DXEngine Crack

Xiaohei leetcode surfing: 94. Inorder traversal of binary tree

基于Appian低代码平台开发一个SpaceX网站
随机推荐
2022牛客暑期多校训练营5(BCDFGHK)
After another 3 days, I have sorted out 90 NumPy examples, and I can't help but bookmark it!
OPENCV学习DAY8
Ab3d.PowerToys and Ab3d.DXEngine Crack
Develop a SpaceX website based on the Appian low-code platform
请你说一下final关键字以及static关键字
对“为什么一些程序员很傲慢”的解读
三、实战---爬取百度指定词条所对应的结果页面(一个简单的页面采集器)
一点点读懂cpufreq(二)
头脑风暴:完全背包
为何越来越多人选择进入软件测试行业?深度剖析软件测试的优势...
golang 协程的实现原理
NebulaGraph v3.2.0 Release Note, many optimizations such as the performance of querying the shortest path
建模师经验分享:模型学习方法
3. Actual combat---crawl the result page corresponding to Baidu's specified entry (a simple page collector)
Basic web in PLSQL
Shell expect real cases
[Cultivation of internal skills of string functions] strncpy + strncat + strncmp (2)
生产者消费者问题
Mathematical Principles of Matrix