当前位置:网站首页>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");
}
}
边栏推荐
- VSCode使用 - Remote-SSH 配置说明
- A little experience of next (ITER (dataloader))
- 用redis做用户访问数据统计HyperLogLog及Bitmap高级数据类型
- 国信证券怎么开户?通过链接办理股票开户安全吗
- [ten thousand words summary] starting from the end, analyze in detail how to fill in the college entrance examination volunteers
- Niuke network: Design LRU cache structure design LFU cache structure
- next(iter(dataloader))的一点点体会
- 新手炒股开户选哪个证券公司比较好?怎样炒股比较安全??
- MySQL index
- 17.13 supplementary knowledge, thread pool discussion, quantity discussion and summary
猜你喜欢
![[ten thousand words summary] starting from the end, analyze in detail how to fill in the college entrance examination volunteers](/img/77/715454c8203d722e246ed70e1fe0d8.png)
[ten thousand words summary] starting from the end, analyze in detail how to fill in the college entrance examination volunteers

MYSQL的下载与配置 mysql远程操控

Preparing for the Blue Bridge Cup and ccf-csp

sql中ROUND和TRUNCATE的区别(四舍五入还是截取小数点后几位)

14《MySQL 教程》INSERT 插入数据

Detailed explanation of asymmetric cryptosystem

RSA加密解密详解

Binary search-1

Dos et détails de la méthode d'attaque

MySql 导出数据库中的全部表索引
随机推荐
How about opening a flush account? Is it safe? How to open a stock trading account
LM06丨仅用成交量构造抄底摸顶策略的奥秘
JS common regular expressions
I want to know. I am in Zhaoqing. Where can I open an account? Is it safe to open an account online?
临时关闭MySQL缓存
The difference between round and truncate in SQL (round or truncate)
接水面试题
Decision tree and random forest
transforms.RandomCrop()的输入只能是PIL image 不能是tensor
vutils.make_grid()与黑白图像有关的一个小体会
MySQL的MVCC机制详解
DoS及攻擊方法詳解
MySQL exports all table indexes in the database
Map and filter methods for processing scarce arrays
Dos et détails de la méthode d'attaque
[QNX] Command
mysql Add column 失败 因为之前有数据,不是默认null 不行
wechat_ Solve the problem of page Jump and parameter transfer by navigator in wechat applet
next(iter(dataloader))的一点点体会
[code Capriccio - dynamic planning] t583. Deleting two strings