当前位置:网站首页>tag动态规划-刷题预备知识-2. 0-1背包理论基础和二维数组解法模板
tag动态规划-刷题预备知识-2. 0-1背包理论基础和二维数组解法模板
2022-06-26 18:01:00 【菜菜的大数据开发之路】
面试常考察的背包问题

在看下面之前, 有必要通过一篇文章, 来熟悉背包问题填表过程: [点我](https://www.yuque.com/docs/share/2fbdf1a7-1499-4696-ab71-1df56240d2d1?# 《动态规划-背包问题》)
一, 0-1背包问题
1. 确定dp数组以及下标的含义
dp[i][j], 其中i代表的是物品, j指代的是背包的容量
如dp[i - 1][j] 表示从编号为
0~i-1的物品中任意选择, 放入到容量为j的背包, 价值总和dp[i -1][j] 最大为多少。

2. 确定递推公式

3. dp数组如何初始化

4. 确定遍历顺序

5. 举例推导dp数组

A. 二维数组的0-1背包模板
[代码实现]
public static void main(String[] args) {
int[] weight = {
1, 3, 4};
int[] value = {
15, 20, 30};
int bagsize = 4;
testweightbagproblem(weight, value, bagsize);
}
public static void testweightbagproblem(int[] weight, int[] value, int bagsize){
int wlen = weight.length, value0 = 0;
//定义dp数组:dp[i][j]表示背包容量为j时,前i个物品能获得的最大价值
int[][] dp = new int[wlen + 1][bagsize + 1];
//初始化:背包容量为0时,能获得的价值都为0
for (int i = 0; i <= wlen; i++){
dp[i][0] = value0;
}
//遍历顺序:先遍历物品,再遍历背包容量
for (int i = 1; i <= wlen; i++){
for (int j = 1; j <= bagsize; j++){
if (j < weight[i - 1]){
dp[i][j] = dp[i - 1][j];
}else{
dp[i][j] = Math.max(dp[i - 1][j], dp[i - 1][j - weight[i - 1]] + value[i - 1]);
}
}
}
//打印dp数组
for (int i = 0; i <= wlen; i++){
for (int j = 0; j <= bagsize; j++){
System.out.print(dp[i][j] + " ");
}
System.out.print("\n");
}
}
边栏推荐
- 17.13 补充知识、线程池浅谈、数量谈、总结
- Halcon's region: features of multiple regions (5)
- RSA概念详解及工具推荐大全 - lmn
- ZCMU--1367: Data Structure
- I want to know. I am in Zhaoqing. Where can I open an account? Is it safe to open an account online?
- ZCMU--1367: Data Structure
- KDD 2022 | 如何在跨域推荐中使用对比学习?
- 力扣每日一题-第28天-566.重塑矩阵
- Case study of row lock and isolation level
- wechat_ Solve the problem of page Jump and parameter transfer by navigator in wechat applet
猜你喜欢

pycharm如何修改多行注释快捷键

mysql Add column 失败 因为之前有数据,不是默认null 不行

Various types of gypsum PBR multi-channel mapping materials, please collect them quickly!

in和exsits、count(*)查询优化

数字签名论述及生成与优点分析

Leetcode - 226. Retourner l'arbre binaire (bfs)

pycharm的plt.show()如何保持不关闭

vutils.make_grid()与黑白图像有关的一个小体会
![[uniapp] the uniapp mobile terminal uses uni Troubleshooting of navigateback failure](/img/26/da00e70d0955bcdef3362474bc5fc7.png)
[uniapp] the uniapp mobile terminal uses uni Troubleshooting of navigateback failure

Detailed explanation of dos and attack methods
随机推荐
ZCMU--1367: Data Structure
二分查找-2
在国金证券开户怎么样?开户安全吗?
KDD 2022 | how to use comparative learning in cross domain recommendation?
物联网协议的王者:MQTT
Plt How to keep show() not closed
How about opening a flush account? Is it safe? How to open a stock trading account
Connected to surface test questions
[buuctf.reverse] 126-130
vutils. make_ A little experience of grid () in relation to black and white images
Vscode usage - Remote SSH configuration description
Detailed explanation of MySQL mvcc mechanism
【动态规划】剑指 Offer II 091. 粉刷房子
transforms.RandomCrop()的输入只能是PIL image 不能是tensor
Concept and working principle of data encryption standard (DES)
ZCMU--1367: Data Structure
深层次安全定义剖析及加密技术
数字签名标准(DSS)
国信证券怎么开户?通过链接办理股票开户安全吗
Padding percentage operation