当前位置:网站首页>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 + " ");
}
}
边栏推荐
- uniapp sharing function - share to friends group chat circle of friends effect (sorting)
- Vscode连接远程服务器(一套配置成功)
- DNS常见资源记录类型详解
- 再肝3天,整理了90个 NumPy 例子,不能不收藏!
- The Go Programming Language (Introduction)
- [Cultivation of internal skills of memory operation functions] memcpy + memmove + memcmp + memset (4)
- Since a new byte of 20K came out, I have seen what the ceiling is
- MySQL基础篇【子查询】
- Will we still need browsers in the future?(feat. Maple words Maple language)
- MYS-6ULX-IOT 开发板测评——使用 Yocto 添加软件包
猜你喜欢
365天深度学习训练营-学习线路
golang 协程的实现原理
[Happy Qixi Festival] How does Nacos realize the service registration function?
【七夕快乐篇】Nacos是如何实现服务注册功能的?
【软件测试】常用ADB命令
Implementing class target method exception using proxy object execution
Statistical words (DAY 101) Huazhong University of Science and Technology postgraduate examination questions
三、实战---爬取百度指定词条所对应的结果页面(一个简单的页面采集器)
Uniapp dynamic sliding navigation effect demo (finishing)
2022牛客暑期多校训练营5(BCDFGHK)
随机推荐
NebulaGraph v3.2.0 Release Note,对查询最短路径的性能等多处优化
After another 3 days, I have sorted out 90 NumPy examples, and I can't help but bookmark it!
@Async注解的作用以及如何实现异步监听机制
MongoDB权限验证开启与mongoose数据库配置
游戏3D建模入门,有哪些建模软件可以选择?
大师教你3D实时角色制作流程,游戏建模流程分享
[Cultivation of internal skills of string functions] strlen + strstr + strtok + strerror (3)
【无标题】
what is MVCC
Mathematical Principles of Matrix
2022/8/3
线程三连鞭之“线程的状态”
Cython
一点点读懂Thremal(二)
LeetCode Hot 100
[Cultivation of internal skills of string functions] strncpy + strncat + strncmp (2)
uniapp 分享功能-分享给朋友群聊朋友圈效果(整理)
PID Controller Improvement Notes No. 7: Improve the anti-overshoot setting of the PID controller
深度|医疗行业勒索病毒防治解决方案
The Go Programming Language (Introduction)