当前位置:网站首页>[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];
}
}
边栏推荐
- 【IDEA】IDEA 格式化 代码技巧 idea 格式化 会加 <p> 标签
- [electron] 基础学习
- Summary of various loams (laser SLAM)
- Classification of cifar-10 dataset with pytorch
- 企业架构师面试的100个问题
- Realization of kaggle cat dog recognition by pytorch
- How to start ID from 1 after MySQL deletes a table
- What problems should be paid attention to in the serpentine wiring of PCB?
- Detect objects and transfer images through mqtt
- NDSS 2022 接收的列表
猜你喜欢
随机推荐
Discuz小鱼游戏风影传说商业GBK+UTF8版模板/DZ游戏网站模板
Azure Kinect DK realizes 3D reconstruction (PC non real time version)
Vivado FFT IP的使用说明
MySQL十八:写语句的执行过程
ABAP essay - material master data interface enhancement - tab enhancement
Open source of local run / development library of hiplot online drawing tool
Practice torch FX: pytorch based model optimization quantization artifact
Usage of vivado vio IP
Is it safe for GF futures to open an account?
Introduction to quantitative trading
Fsnotify interface of go language to monitor file modification
移动端避免使用100vh[通俗易懂]
[electron] 基础学习
Azure Kinect DK realizes 3D reconstruction (Jetson real-time version)
Spark BUG實踐(包含的BUG:ClassCastException;ConnectException;NoClassDefFoundError;RuntimeExceptio等。。。。)
在线JSON转PlainText工具
Discuz淘宝客网站模板/迪恩淘宝客购物风格商业版模板
跟着存档教程动手学RNAseq分析(四):使用DESeq2进行DE分析的QC方法
Advertising is too "wild", Yoshino "surrenders"
clickonce 部署ClickOnce应用程序时出错-清单中的引用与下载的程序集的标识不匹配









