当前位置:网站首页>[leetcode refers to offer 47. Maximum value of gift (medium)]
[leetcode refers to offer 47. Maximum value of gift (medium)]
2022-04-23 21:21:00 【Minaldo7】
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 ?
Example 1:
Input :
[
[1,3,1],
[1,5,1],
[4,2,1]
]
Output : 12
explain : route 1→3→5→2→1 Can get the most value gifts
Tips :
0 < grid.length <= 200
0 < grid[0].length <= 200
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/li-wu-de-zui-da-jie-zhi-lcof
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
The problem solving process :
Dynamic programming
class Solution {
public int maxValue(int[][] grid) {
int m=grid.length,n=grid[0].length;
int dp[] = new int[n+1];
int max = grid[0][0];
for(int i=1;i<=m;i++){
for(int j=1;j<=n;j++){
dp[j] = Math.max(dp[j-1],dp[j]) + grid[i-1][j-1];
}
}
return dp[n];
}
}
Execution results :
版权声明
本文为[Minaldo7]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/111/202204210544479416.html
边栏推荐
- go map
- opencv应用——以图拼图
- Solve importerror: cannot import name 'imread' from 'SciPy misc‘
- Ubutnu20 installer centernet
- Realrange, reduce, repeat and einops in einops package layers. Rearrange and reduce in torch. Processing methods of high-dimensional data
- 浅谈数据库设计之三大范式
- ubutnu20安装CenterNet
- ROS learning notes - tutorial on the use of ROS
- Pipes and xargs
- [SDU chart team - core] enumeration of SVG attribute class design
猜你喜欢
MySQL基础之写表(创建表)
Fastdfs思维导图
Chrome 94 引入具有争议的 Idle Detection API,苹果和Mozilla反对
flomo软件推荐
Reentrant function
Deep analysis of C language function
浅谈数据库设计之三大范式
Common problems in deploying projects with laravel and composer for PHP
Centos7 builds MySQL master-slave replication from scratch (avoid stepping on the pit)
Write table of MySQL Foundation (create table)
随机推荐
Minecraft 1.12.2 module development (43) custom shield
Thinkphp5 + data large screen display effect
Write table of MySQL Foundation (create table)
On the three paradigms of database design
管道和xargs
wait、waitpid
setInterval、setTimeout、requestAnimationFrame
引入结构化并发,Swift 5.5 发布!
Graph traversal - BFS, DFS
学会打字后的思考
pikachuxss如何获取cookie靶场,返回首页总是失败
Pipes and xargs
Centos7 builds MySQL master-slave replication from scratch (avoid stepping on the pit)
Ubutnu20 installer centernet
Tensorflow realizes gradient accumulation, and then returns
Opencv reports an error. Expected PTR < CV:: UMAT > for argument '% s'‘
presto on spark 支持3.1.3记录
启牛学堂有用吗,推荐的证券账户是否安全
DeNO 1.13.2 release
一些接地气的话儿