当前位置:网站首页>剑指 Offer 47. 礼物的最大价值(DP)
剑指 Offer 47. 礼物的最大价值(DP)
2022-06-28 01:47:00 【BugMaker-shen】

二维dp

当前格的最大价值(dp[i][j])等于当前格子价值(grid[i][j])加上左边格子最大价值(dp[i][j-1])和上边格子最大价值(dp[i-1][j])较大者
class Solution {
public:
int maxValue(vector<vector<int>>& grid) {
int m = grid.size();
int n = grid[0].size();
// 开辟和grid一样大的dp数组
vector<vector<int>> dp(m, vector<int>(n, 0));
// 初始化第0行和第0列的礼物最大值
dp[0][0] = grid[0][0];
for(int j = 1; j < n; j++){
dp[0][j] += dp[0][j-1] + grid[0][j];
}
for(int i = 1; i < m; i++){
dp[i][0] += dp[i-1][0] + grid[i][0];
}
// 从[1][1]开始逐行求出礼物最大值
for(int i = 1; i < m; i++){
for(int j = 1; j < n; j++){
dp[i][j] = grid[i][j] + max(dp[i-1][j], dp[i][j-1]);
}
}
return dp[m-1][n-1];
}
};
简化边界处理:我们多开辟一行一列,并将第0行和第0列的值初始化为0,这样就能统一成grid当前值+max(上方最大价值,左侧最大价值)
class Solution {
public:
int maxValue(vector<vector<int>>& grid) {
int m = grid.size();
int n = grid[0].size();
vector<vector<int>> dp(m + 1, vector<int>(n + 1, 0));
// i与一维dp数组无关
for(int i = 1; i <= m; i++){
for(int j = 1; j <= n; j++){
dp[i][j] = grid[i-1][j-1] + max(dp[i][j-1], dp[i-1][j]);
}
}
return dp[m][n];
}
};
滚动数组降维
把紫色部分当成一维dp数组

class Solution {
public:
int maxValue(vector<vector<int>>& grid) {
int m = grid.size();
int n = grid[0].size();
vector<int> dp(n + 1, 0);
for(int i = 0; i < m; i++){
for(int j = 1; j <= n; j++){
dp[j] = max(dp[j-1], dp[j]) + grid[i][j-1];
}
}
return dp[n];
}
};
边栏推荐
- Which securities platform is the best and safest for a novice to open a stock trading account
- 分布式事务解决方案Seata-Golang浅析
- 为什么大厂压力大,竞争大,还有这么多人热衷于大厂呢?
- Mixed programming of C language and assembly language in stm32
- 【插件-statistic】统计代码行数和相关数据
- [522. longest special sequence II]
- [today in history] June 19: iPhone 3GS launched; Pascal was born; Anti terrorist elite begins testing
- 【Kotlin】在Android官方文档中对其语法的基本介绍和理解
- 字节跳动面试官:一张图片占据的内存大小是如何计算
- PHP 代码 微信、公众号、企业微信 发送表情符号 [U+1F449]
猜你喜欢

被校园暴力,性格内向的马斯克凄惨而励志的童年
![[today in history] June 15: the first mobile phone virus; AI master simahe was born; Chromebook launch](/img/d4/413c84a75f16a09867cfaa3d7f8736.png)
[today in history] June 15: the first mobile phone virus; AI master simahe was born; Chromebook launch

音视频技术开发周刊 | 251

视频编解码性能优化与实现

R language penalty logistic regression, linear discriminant analysis LDA, generalized additive model GAM, multiple adaptive regression splines Mars, KNN, quadratic discriminant analysis QDA, decision
![[today in history] June 24: Netease was established; The first consumer electronics exhibition was held; The first webcast in the world](/img/f7/b3239802d19d00f760bb3174649a89.jpg)
[today in history] June 24: Netease was established; The first consumer electronics exhibition was held; The first webcast in the world

How to judge that the thread pool has completed all tasks?

将PCAP转换为Json文件的神器:joy(安装篇)

CMU puts forward a new NLP paradigm - reconstructing pre training, and achieving 134 high scores in college entrance examination English
![[today in history] June 19: iPhone 3GS launched; Pascal was born; Anti terrorist elite begins testing](/img/ec/90961351a0de1eac26dd003bf25d34.png)
[today in history] June 19: iPhone 3GS launched; Pascal was born; Anti terrorist elite begins testing
随机推荐
拾光者,云南白药!
The first in the industry! MOS sub evaluation model for subjective video quality experience that can run on mobile devices!
微信小程序中生成二维码
[today in history] June 7: kubernetes open source version was released; Worldofwarcraft landed in China; Birth of the inventor of packet switching network
[today in history] June 10: Apple II came out; Microsoft acquires gecad; The scientific and technological pioneer who invented the word "software engineering" was born
业内首个!可运行在移动设备端的视频画质主观体验MOS分评估模型!
JDBC and MySQL databases
Interview: how do lists duplicate objects according to their attributes?
Intel Ruixuan A380 graphics card will be launched in China
Arduino esp8266 web LED control
您的物联网安全性是否足够强大?
[today in history] June 20: the father of MP3 was born; Fujitsu was established; Google acquires dropcam
Built in functions for MySQL database operations
【Kotlin】在Android官方文档中对其语法的基本介绍和理解
Embedded DSP audio development
基于流的深度生成模型
Which securities platform is the best and safest for a novice to open a stock trading account
R language penalty logistic regression, linear discriminant analysis LDA, generalized additive model GAM, multiple adaptive regression splines Mars, KNN, quadratic discriminant analysis QDA, decision
Agileplm exception resolution session
Mixed programming of C language and assembly language in stm32