当前位置:网站首页>DP背包问题
DP背包问题
2022-07-20 03:23:00 【库里不会投三分】
0-1 背包问题
经典的DP问题,还是四步走,明确状态——>明确选择——> 定义 dp 数组/函数的含义 ——>base case
明确状态
- 首先会变化的就是背包的容量
- 其次会变的就是能够选择的物品
明确选择
- 对于一个物品,要么不选
- 要么就是选
明确定义dp数组含义
- dp[i][j]表示可以选择前i个物品,j为表示当前背包的容量,dp[i][j]表示能装的最大价值
明确base case
- dp[0][i]表示可选择的物品为0,肯定最大价值为0
- dp[i][0]表示,容量为0,最大价值肯定为0
核心——写出状态转换方程
代码
public int backPackII(int m, int[] a, int[] v) { // write your code here int row=a.length; int column=m; //base case dp[0][0...column-1] dp[0...row-1][0] int dp[][]=new int[row+1][column+1]; for (int i = 1; i <=row; i++) { for (int j = 1; j <=column; j++) { if(j<a[i-1]){ dp[i][j]=dp[i-1][j]; }else { dp[i][j]=Math.max(dp[i-1][j],dp[i-1][j-a[i-1]]+v[i-1]); } } } return dp[row][column]; }
边栏推荐
- Unity shader implements the image with rounded corners and edge borders
- 【AD学习记录】覆铜
- Redis 主从复制&哨兵模式
- Matlab summary of differential equation solving methods
- About: Customizing templates in office 2021
- MySQL 事务管理
- Harbor high availability cluster design and deployment (offline installation, including video)
- . Net core rapid development platform, powerful workflow engine, multi system rapid configuration
- How to create a plug-in for QML through cmake
- A good resume can always brighten people's eyes during the interview of the testing post
猜你喜欢
【upload靶场12-16】截断、图片马
从三翼鸟,透视家居品牌的“飞轮效应”
Lecture 5 of Data Engineering Series: data set quality of data centric AI
vben-admin 时间选择器相关配置以及设置不可选择的时间
An interesting example to illustrate the difference of emplace_back() and push_back()
请问产品经理下班后是如何安排时间的?
MySQL transaction management
美国议员倡导打击加密挖矿 敲响加密警钟?减少碳足迹才能发挥真正价值
10 port scanning tools for advanced scanning by network administrators
Merge and sort targeted questions
随机推荐
某网站登录接口password参数还原
【obs】Transform: fit to screen
Compile + link and preprocess
Laravel定时任务
波场联合储备发表公开信,强调USDD不受制于任何中心化机构
Use Amazon memorydb for redis as the metadata engine of juicefs
30-Spark入门之Spark技术栈讲解、分区、系统架构、算子和任务提交方式
亮点抢先看!2022开放原子全球开源峰会定于7月25-29日在北京举办
Understand image storage and image security from image warehouse tools, image download acceleration tools, and security scanning tools
V4l2 learning notes
How to create a plug-in for QML through cmake
技术干货 | 解决面试中80%问题,基于MindSpore实现AUC/ROC
服务器的1U、2U、4U是指什么?
【已解决】org.apache.catalina.LifecycleException: 无法启动组件[StandardEngine[Catalina].StandardHost[localhost]
What parameters do you need to see when buying a server and how to see the server configuration
技术干货 | 基于 MindSpore 实现图像分割之平均表面距离
[leetcode] sword finger offer 53 - I. find the number I in the sorted array
单体 or 微服务?你以为是架构权衡?其实是认知负载!
alert日志告警“minact-scn: useg scan erroring out with error e:12751”处理
C语言实现二叉树线索化