当前位置:网站首页>LeetCode刷题系列 -- 剑指 Offer 47. 礼物的最大价值
LeetCode刷题系列 -- 剑指 Offer 47. 礼物的最大价值
2022-07-22 00:03:00 【在河之洲木水】
在一个 m*n 的棋盘的每一格都放有一个礼物,每个礼物都有一定的价值(价值大于 0)。你可以从棋盘的左上角开始拿格子里的礼物,并每次向右或者向下移动一格、直到到达棋盘的右下角。给定一个棋盘及其上面的礼物的价值,请计算你最多能拿到多少价值的礼物?
示例 1:
输入:
[
[1,3,1],
[1,5,1],
[4,2,1]
]
输出: 12
解释: 路径 1→3→5→2→1 可以拿到最多价值的礼物
提示:
0 < grid.length <= 200
0 < grid[0].length <= 200
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/li-wu-de-zui-da-jie-zhi-lcof
思路:
此题与 64. 最小路径和 是相似的问题
java代码
class Solution {
public int maxValue(int[][] grid) {
int rowLen = grid.length;
int colmLen = grid[0].length;
int[][] dp = new int[rowLen][colmLen];
// 1. 初始化 dp
for (int i=0;i< rowLen;i++) {
if(i==0) {
dp[i][0] = grid[0][0];
} else {
dp[i][0] = dp[i-1][0] + grid[i][0];
}
}
for (int j=0;j< colmLen;j++) {
if(j==0) {
dp[0][j] = grid[0][0];
} else {
dp[0][j] = dp[0][j-1] + grid[0][j];
}
}
// 2. 转态转移
for(int i=1;i< rowLen;i++) {
for(int j=1;j<colmLen;j++) {
dp[i][j] = Math.max(dp[i-1][j],dp[i][j-1]) + grid[i][j];
}
}
return dp[rowLen-1][colmLen-1];
}
}
边栏推荐
- Leetcode-2337: move the fragment to get the string
- Job hopping? Have you got the 7 skills you need to master in the interview software test?
- Live review | wonderful playback of Apache pulsar meetup (including PPT download)
- Leetcode-6116: calculate the value of Boolean binary tree
- Notes and Thoughts on the red dust of the curtain of Heaven (II) you know what others know and what you know
- TiDB 最佳实践
- 简易消息队列实现 Nodejs + Redis =MQ
- PD 调度策略最佳实践
- Analyze the capabilities and scenarios of Apache pulsar, a cloud native message flow system
- Take you to brush (niuke.com) C language hundred questions (day 3)
猜你喜欢
架构师进阶,微服务设计与治理的 16 条常用原则
Leetcode-1260: 2D mesh migration
来get一个超实用的小页面——todolist
Network metering - transport layer
基于 ABP 实现 DDD-- 领域服务、应用服务和 DTO 实践
After reading this, I can't DVMA yet. Please eat melon
基于SSM+JSP+MYSQL+H-UI 实现的火锅店点餐系统
IDEA反编译出整个jar包源码
Do you have to follow flush privileges after MySQL grant?
易基因 | 简化基因组DNA甲基化测序(RRBS)实验怎么做?
随机推荐
Capacitive touch chip used in touch panel
Learn the use of mockmvc from the source code
MySQL grant之后一定要跟随flush privileges么?
看完这个,还不会DVMA,请你吃瓜
跳槽?面试软件测试需要掌握的7个技能Get了吗?
JUC concurrent contracting
社区活动 | Apache Doris 毕业庆典活动圆满结束!
Auto. JS learning note 18: sub thread and timer are used together (an example is at the end)
Super dry! Thoroughly understand the differences and connections between simplex, half duplex and full duplex
Leetcode-386: number of dictionary rows
Notes and Thoughts on the red dust of the curtain of Heaven (II) you know what others know and what you know
MySQL show profile diagnostic SQL and configuration optimization
谈谈程序员如何提高自己的写作能力
Pytorch(三)——FashionMNIST时装分类
Leetcode-1260: 2D mesh migration
三节点混合部署的最佳实践
After reading this, I can't DVMA yet. Please eat melon
PyTorch(四)——PyTorch模型定义
Apachecon Asia 2022 opens registration: pulsar technology issues make a big debut
Final consistency distributed transaction TCC