当前位置:网站首页>[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
边栏推荐
- Pipes and xargs
- Leetcode-279-complete square number
- Send email to laravel
- pikachuxss如何获取cookie靶场,返回首页总是失败
- How to learn software testing? Self study or training? After reading this article, you will understand
- 小米手机全球已舍弃“MI”品牌,全面改用“xiaomi”全称品牌
- setInterval、setTimeout、requestAnimationFrame
- Deep analysis of C language pointer (Part I)
- Unit function expansion
- How to play the guiding role of testing strategy
猜你喜欢

浅谈数据库设计之三大范式

CUDA, NVIDIA driver, cudnn download address and version correspondence

Google 尝试在 Chrome 中使用 Rust

flomo软件推荐

Prim、Kruskal

thinkphp5+数据大屏展示效果

Xiaomi mobile phone has abandoned the "Mi" brand all over the world and switched to the full name brand of "Xiaomi"

41. The first missing positive number

Minecraft 1.12.2 module development (43) custom shield

Flomo software recommendation
随机推荐
Pikachuxss how to get cookie shooting range, always fail to return to the home page
The more you use the computer, the slower it will be? Recovery method of file accidental deletion
MySQL进阶之表的增删改查
Recommended usage scenarios and production tools for common 60 types of charts
Rust更适合经验较少的程序员?
Selenium displays webdriverwait
Lunch on the 23rd day at home
Alibaba cloud responded to the disclosure of user registration information
Factory mode
ROS learning notes - tutorial on the use of ROS
Norm normalization in tensorflow and pytorch of records
3-5通过XSS获取cookie以及XSS后台管理系统的使用
Google tries to use rust in Chrome
Some thoughts on super in pytorch, combined with code
Tensorflow1. X and 2 How does x read those parameters saved in CKPT
Awk example skills
Pyuninstaller package exe cannot find the source code when running, function error oserror: could not get source code
Centralized record of experimental problems
Keywords static, extern + global and local variables
Thinking after learning to type