当前位置:网站首页>[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];
}
}
边栏推荐
- Livox Lidar+APX15 实时高精度雷达建图复现整理
- Usage of vivado vio IP
- [从零开始学习FPGA编程-48]:视野篇 - 智能传感器的发展与应用
- 「R」使用ggpolar绘制生存关联网络图
- EasyCVR平台路由日志功能的技术实现过程【附代码】
- Using xgboost with tidymodels
- The choice and trade-off between vector recall and literal recall
- Advertising is too "wild", Yoshino "surrenders"
- [js]var, let,const 的区别
- Getting started with pytorch
猜你喜欢

Practice torch FX: pytorch based model optimization quantization artifact

Livox lidar+ Haikang camera generates color point cloud in real time

vivado VIO IP的用法

小程序referer

云辅助隐私集合求交(Server-Aided PSI)协议介绍:学习

使用SQL进行数据去重的N种方法

【IDEA】IDEA 格式化 代码技巧 idea 格式化 会加 <p> 标签

The most illusory richest man in China is even more illusory

【微服务|Sentinel】sentinel数据持久化

EasyCVR平台路由日志功能的技术实现过程【附代码】
随机推荐
Batch processing - Excel import template 1.1- support multiple sheet pages
Swing UI——容器(一)
Web worker introduction and use cases
Hiplot 在線繪圖工具的本地運行/開發庫開源
官宣!Apache Doris 从 Apache 孵化器毕业,正式成为 Apache 顶级项目!
因美纳陷数据泄露“丑闻”:我国基因数据安全能交给美企吗?
Liuleifeng, a "good man in Guangzhou" in the first quarter of 2022, has a strong sense of integrity and food safety
新加坡国立大学|采用无模型强化学习方法评估能源效益数据中心的节能情况
Livox Lidar+海康Camera 基于loam的实时三维重建生成RGB彩色点云
webService
跟着存档教程动手学RNAseq分析(三):使用DESeq2进行计数标准化
Working at home is more tiring than going to work at the company?
Online JSON to plaintext tool
MySQL十八:写语句的执行过程
Practice torch FX: pytorch based model optimization quantization artifact
To build a "strong core" in Nansha, the first IC Nansha conference was held in Nansha
ABAP随笔-关于ECC后台server读取Excel方案的想法
Summary of solutions to cross system data consistency problems
OData - API using SAP API hub in SAP S4 op
个人TREE ALV 模版-加快你的开发