当前位置:网站首页>Complete knapsack problem (template)
Complete knapsack problem (template)
2022-07-05 00:23:00 【The left wheel of fate】
List of articles
List of articles
Summary of problems
There's a backpack , The maximum bearing capacity is N, Now we need to pack some items i( The value of the item is value[i], The weight of the article is weight[i]), There is no limit to the number of items ( That is, it can be installed repeatedly ), Ask for the maximum value of these items packed on this back .
Complete backpack features
Each item has an infinite number of
Completely backpack ( One dimensional rolling array version )
analysis
- Ahead 01 Backpack we can know , Items circulating outside are in positive order , Internal reverse circulation backpack . Internal reverse circulation is to ensure that items can only be added once , But the complete backpack has infinite items , It shows that my backpack can traverse forward , There is nothing wrong with the superposition of sub problems .
- 01 2D in backpack dp Two of the arrays for The order of traversal can be reversed , A one-dimensional dp Two of the arrays for The sequence of the cycle must first traverse the items , Then traverse the backpack capacity .
- In a complete backpack , For one dimension dp Array , In fact, two for The nesting order of loops can be reversed
- therefore , Whether it's 01 Backpack or full backpack , We usually go through items first , And then traverse the backpack ,
Be careful : Here is general
Code
# Completely backpack ( One dimensional rolling array version )
weight =[1,3,4]
value = [15,20,30]
Max = 4 # The maximum capacity of the backpack
dp = [0]*5 # dp[j] Indicates that the backpack capacity is j The maximum value of is dp[j]
for i in range(3):
for j in range(weight[i],5):
dp[j] = max(dp[j],dp[j-weight[i]]+value[i])
print(dp)
The final results
So the capacity is 4 The backpack , The greatest value is 60
If there is a mistake , Please correct me , Welcome to exchange , thank you *(・ω・)ノ
边栏推荐
- IELTS examination process, what to pay attention to and how to review?
- TS快速入门-函数
- Actual combat simulation │ JWT login authentication
- lambda expressions
- JS convert pseudo array to array
- IT转测试岗,从迷茫到坚定我究竟付出了什么?
- Power operation and maintenance cloud platform: open the new mode of "unattended and few people on duty" of power system
- [paper reading] cavemix: a simple data augmentation method for brain vision segmentation
- [IELTS reading] Wang Xiwei reads P4 (matching2 paragraph information matching question [difficult])
- If you open an account of Huatai Securities by stock speculation, is it safe to open an account online?
猜你喜欢
多回路仪表在基站“转改直”方面的应用
2022.07.03 (lc_6111_counts the number of ways to place houses)
Learning of basic amplification circuit
Get to know ROS for the first time
如何用快解析自制IoT云平台
【路径规划】RRT增加动力模型进行轨迹规划
He worked as a foreign lead and paid off all the housing loans in a year
企业公司项目开发好一部分基础功能,重要的事保存到线上第一a
The waterfall flow layout demo2 (method 2) used by the uniapp wechat applet (copy and paste can be used without other processing)
In June, the list of winners of "Moli original author program" was announced! Invite you to talk about the domestic database
随机推荐
Get to know ROS for the first time
2022.07.03 (LC 6108 decryption message)
模板的进阶
Go pit - no required module provides Package: go. Mod file not found in current directory or any parent
How to do the project of computer remote company in foreign Internet?
人脸识别5- insight-face-paddle-代码实战笔记
图解网络:什么是网关负载均衡协议GLBP?
[IELTS reading] Wang Xiwei reads P4 (matching2 paragraph information matching question [difficult])
JS how to realize array to tree
分布式BASE理论
[paper reading] cavemix: a simple data augmentation method for brain vision segmentation
《论文笔记》Multi-UAV Collaborative Monocular SLAM
【C】 (written examination questions) pointer and array, pointer
Verilog tutorial (11) initial block in Verilog
Hologres Query管理及超时处理
go踩坑——no required module provides package : go.mod file not found in current directory or any parent
Date time type and format in MySQL
如何将自己的代码作品快速存证,已更好的保护自己劳动成果
GDB常用命令
Learning of basic amplification circuit