当前位置:网站首页>Tag dynamic programming - preliminary knowledge for question brushing -2 0-1 knapsack theory foundation and two-dimensional array solution template
Tag dynamic programming - preliminary knowledge for question brushing -2 0-1 knapsack theory foundation and two-dimensional array solution template
2022-06-26 18:10:00 【Caicai's big data development path】
The knapsack problem that interview often inspects

Before you look down , It is necessary to pass an article , To familiarize yourself with the process of filling out the backpack question : [ Am I ](https://www.yuque.com/docs/share/2fbdf1a7-1499-4696-ab71-1df56240d2d1?# 《 Dynamic programming - knapsack problem 》)
One , 0-1 knapsack problem
1. determine dp The meaning of arrays and subscripts
dp[i][j], among i Represents objects , j It refers to the capacity of the backpack
Such as dp[i - 1][j] Indicates from number
0~i-1 The itemsChoose from any of the following , Put inCapacity of jThe backpack , The sum of values dp[i -1][j] What is the maximum .

2. Determine the recurrence formula

3. dp How to initialize an array

4. Determine the traversal order

5. Give an example to deduce dp Array

A. Two dimensional array 0-1 Backpack template
[ Code implementation ]
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;
// Definition dp Array :dp[i][j] Indicates that the backpack capacity is j when , front i The maximum value an item can get
int[][] dp = new int[wlen + 1][bagsize + 1];
// initialization : The capacity of the backpack is 0 when , The value you can get is 0
for (int i = 0; i <= wlen; i++){
dp[i][0] = value0;
}
// traversal order : Go through the items first , Then traverse the backpack capacity
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]);
}
}
}
// Print dp Array
for (int i = 0; i <= wlen; i++){
for (int j = 0; j <= bagsize; j++){
System.out.print(dp[i][j] + " ");
}
System.out.print("\n");
}
}
边栏推荐
- Map和List<Map>转相应的对象
- JVM入个门(1)
- next(iter(dataloader))的一点点体会
- DoS及攻击方法详解
- 分页查询、JOIN关联查询优化
- Digital signature standard (DSS)
- Which securities company is better for a novice to open a stock trading account? How is it safer to speculate in stocks??
- Prometeus 2.34.0 new features
- 图像二值化处理
- Introduction to Ethereum Technology Architecture
猜你喜欢

wechat_ Solve the problem of page Jump and parameter transfer by navigator in wechat applet

RSA概念详解及工具推荐大全 - lmn

Introduction to Ethereum Technology Architecture

接水面试题

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

LM06丨仅用成交量构造抄底摸顶策略的奥秘

CD-CompactDisk

Preparing for the Blue Bridge Cup and ccf-csp

Let torch cuda. is_ Experience of available() changing from false to true

tag动态规划-刷题预备知识-2. 0-1背包理论基础和二维数组解法模板
随机推荐
Data Encryption Standard DES security
小程序设置按钮分享功能
JS cast
Tencent qianzhiming: Exploration and application of pre training methods in information flow business
RuntimeError: CUDA error: out of memory自己的解决方法(情况比较特殊估计对大部分人不适用)
清华&商汤&上海AI&CUHK提出Siamese Image Modeling,兼具linear probing和密集预测性能!
RSA加密解密详解
【Unity】在Unity中使用C#执行外部文件,如.exe或者.bat
A little experience of next (ITER (dataloader))
Digital signature standard (DSS)
数字签名标准(DSS)
贝叶斯网络详解
零时科技 | 智能合约安全系列文章之反编译篇
决策树与随机森林
pycharm的plt.show()如何保持不关闭
Connected to surface test questions
ros::spinOnce()和ros::spin()的使用和区别
RSA concept explanation and tool recommendation - LMN
PC端录制扫515地机器人/scan数据
临时关闭MySQL缓存