当前位置:网站首页>[sword finger offer] 47 Maximum value of gifts
[sword finger offer] 47 Maximum value of gifts
2022-06-27 23:26:00 【LuZhouShiLi】
The finger of the sword Offer 47. The greatest value of gifts
subject
In a m*n There is a gift in every space of the chessboard , Every gift has a certain value ( Value greater than 0). You can start from the top left corner of the chessboard to get the gifts in the grid , And move right or down one space at a time 、 Until you reach the bottom right corner of the chessboard . Given the value of a chessboard and its gifts , Please calculate the maximum value of gifts you can get ?
Ideas
- State definition : Let's set the dynamic programming matrix dp,dp(i,j) Represents starting from the upper left corner of the chessboard , Reach cell (i,j) The maximum cumulative value of a gift when you get it .
- Transfer equation :
- When i = 0 And j = 0 when , Is the starting element
- When i = 0 And j != 0 when , First row element of matrix , It can only be reached from the left
- When i != 0 And j = 0 when , The first column of the matrix , It can only be reached from the top
- When i != 0 And j != 0 when , It can be reached from the left or above

Code
class Solution {
public int maxValue(int[][] grid) {
int m = grid.length,n = grid[0].length;// Take out the number of rows or columns of the matrix
for(int i = 0; i < m; i++)
{
for(int j = 0; j < n; j++)
{
if(i == 0 && j == 0) continue;
if(i == 0) grid[i][j] = grid[i][j] + grid[i][j - 1];// From the left to
else if(j == 0)
{
grid[i][j] = grid[i][j] + grid[i - 1][j];
}
else{
grid[i][j] += Math.max(grid[i][j - 1],grid[i - 1][j]);
}
}
}
return grid[m - 1][n - 1];
}
}
边栏推荐
- 基于 ESXi 的黑群晖 DSM 7.0.1 安装 VMware Tools
- [Blue Bridge Cup training 100 questions] scratch digital calculation Blue Bridge Cup competition special prediction programming question collective training simulation exercise question No. 16
- Online JSON to plaintext tool
- Is it safe for GF futures to open an account?
- Download versions such as typora 1.2.5
- Summary of solutions to cross system data consistency problems
- 新加坡国立大学|采用无模型强化学习方法评估能源效益数据中心的节能情况
- golang - new和make的区别
- This year's examinees are more "desperate" than the college entrance examination
- Is it safe to use flush mobile phones to speculate in stocks?
猜你喜欢
随机推荐
Bibliothèque d'exploitation / de développement locale open source pour l'outil de dessin en ligne hiplot
凌云出海记 | 沐融科技&华为云:打造非洲金融SaaS解决方案样板
打造南沙“强芯”,南沙首届IC Nansha大会召开
Detect objects and transfer images through mqtt
Online JSON to plaintext tool
Livox Lidar+海康Camera 基于loam的实时三维重建生成RGB彩色点云
电子科大(申恒涛团队)&京东AI(梅涛团队)提出用于视频问答的结构化双流注意网络,性能SOTA!优于基于双视频表示的方法!
【你真的会用ES吗】ES基础介绍(二)
云辅助隐私集合求交(Server-Aided PSI)协议介绍:学习
Batch processing - Excel import template 1.1- support multiple sheet pages
支持删除,更新任意结点的优先级队列
[随笔]ME53N 增加按钮,调用URL
webService
Livox Lidar+海康Camera实时生成彩色点云
实践torch.fx:基于Pytorch的模型优化量化神器
The latest cloud development wechat balance charger special effect applet source code
「R」使用ggpolar绘制生存关联网络图
Working at home is more tiring than going to work at the company?
"Top stream Aidou manufacturing machine" cooperates with four industrial capitals to become LP
Feign通过自定义注解实现路径的转义









