当前位置:网站首页>[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
边栏推荐
- 亚马逊和Epic将入驻,微软应用商城向第三方开放
- Fastdfs mind map
- 一文解决浏览器跨域问题
- Pipes and xargs
- 又一款数据分析神器:Polars 真的很强大
- Is rust more suitable for less experienced programmers?
- GSI-ECM工程建设管理数字化平台
- Addition, deletion, modification and query of advanced MySQL data (DML)
- The more you use the computer, the slower it will be? Recovery method of file accidental deletion
- Some thoughts on super in pytorch, combined with code
猜你喜欢
Minecraft 1.12.2模组开发(四十三) 自定义盾牌(Shield)
1.整理华子面经--1
Chrome 94 introduces the controversial idle detection API, which apple and Mozilla oppose
ROS学习笔记-----ROS的使用教程
Opencv application -- jigsaw puzzle
电脑越用越慢怎么办?文件误删除恢复方法
Reentrant function
DeNO 1.13.2 release
C, print the source program of beautiful bell triangle
3-5通过XSS获取cookie以及XSS后台管理系统的使用
随机推荐
Tensorflow1. X and 2 How does x read those parameters saved in CKPT
Some grounded words
Thinking after learning to type
Preliminary analysis of Airbase
Chrome 94 introduces the controversial idle detection API, which apple and Mozilla oppose
mmap、munmap
Explore ASP Net core read request The correct way of body
41. The first missing positive number
MySQL进阶之数据的增删改查(DML)
Reference of custom message in ROS function pack failed
常用60类图表使用场景、制作工具推荐
go slice
Common problems in deploying projects with laravel and composer for PHP
Solve importerror: cannot import name 'imread' from 'SciPy misc‘
flomo软件推荐
ros功能包内自定义消息引用失败
Addition, deletion, modification and query of advanced MySQL data (DML)
Minecraft 1.12.2模组开发(四十三) 自定义盾牌(Shield)
Problem brushing plan -- dynamic programming (IV)
Is qiniu school useful and is the recommended securities account safe