当前位置:网站首页>jz47 礼物的最大价值(动态规划思路)
jz47 礼物的最大价值(动态规划思路)
2022-07-24 05:22:00 【喜乐自在】

动态规划问题的关键是,寻找dp状态方程,将结果与 dp状态绑定起来,将求解问题划分为若干个子问题,从而求解出结果

对于整个过程,我们先考虑特殊情况,即礼物只沿第0行或者第0列移动,那么礼物价值grid[i][j]+=grid[i][j-1]或者grid[i][j]+=grid[i-1][j],其他情况下 grid[i][j]+=max(grid[i][j-1],grid[i-1][j])
代码如下
int maxValue(vector<vector<int> >& grid) {
// write code here
int col=grid[0].size();
int row=grid.size();
if(row==0)
return 0;
for(int i=0;i<row;i++)
for(int j=0;j<col;j++){
if(j>0&&i>0)
grid[i][j]+=max(grid[i-1][j],grid[i][j-1]);
else if(j>0)
grid[i][j]+=grid[i][j-1];
else if(i>0)
grid[i][j]+=grid[i-1][j];
}
return grid[row-1][col-1];
}
};边栏推荐
- MySQL基础---约束
- QT char to qstring hexadecimal and char to hexadecimal integer
- 碰壁记录(持续更新)
- Xshell remote access tool
- Dameng database_ Summary of supported data types
- Dameng database_ User password policy
- UE4 random generation of items
- Kernel pwn 基础教程之 Heap Overflow
- 本地搭建WordPress个人博客,并内网穿透发布上线 (22)
- 论文阅读-Endmember-Guided Unmixing Network (EGU-Net) 端元指导型高光谱解混网络
猜你喜欢
随机推荐
Installation of tensorflow and pytorch frames and CUDA pit records
如何建立一个仪式感点满的网站,并发布到公网 2-2
碰壁记录(持续更新)
Detailed explanation of KMP code distribution
Accurate calculation of time delay detailed explanation of VxWorks timestamp
IP笔记(8)
Unity Shader :实现漫反射与高光反射
Day2 websocket+ sort
Foundation of JUC concurrent programming (4) -- thread group and thread priority
Using keras to realize LSTM time series prediction based on attention mechanism
HoloLens 2 中文开发文档 MRTK v2
Using keras to realize multidimensional (multivariable) time series prediction of cnn+bilstm+attention
不租服务器,自建个人商业网站(4)
什么是单调队列
The detailed process of connecting MySQL with QT and outputting data to QT window tablewidget.
Sequential stack C language stack entry and exit traversal
Unity Shader从内置渲染管线迁移到URP
Calculation steps of principal component analysis
[principles of database system] Chapter 4 advanced database model: Unified Modeling Language UML, object definition language ODL
Hololens2 development: use MRTK and simulate eye tracking









