当前位置:网站首页>剑指 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];
}
};
边栏推荐
- R语言惩罚逻辑回归、线性判别分析LDA、广义加性模型GAM、多元自适应回归样条MARS、KNN、二次判别分析QDA、决策树、随机森林、支持向量机SVM分类优质劣质葡萄酒十折交叉验证和ROC可视化
- 第一次使用gcc和makefile编写c程序
- JDBC与MySQL数据库
- [today in history] June 11: the co inventor of Monte Carlo method was born; Google launched Google Earth; Google acquires waze
- QEMU monitor usage
- CMU提出NLP新范式—重构预训练,高考英语交出134高分
- Usage details of staticlayout
- Flask Foundation: template inheritance + static file configuration
- 2-5 basic configuration -win2003 add attack surface
- 论文阅读:Generative Adversarial Transformers
猜你喜欢
![[kotlin] basic introduction and understanding of its syntax in Android official documents](/img/44/ec59383ddfa2624a1616d13deda4a4.png)
[kotlin] basic introduction and understanding of its syntax in Android official documents

树莓派-环境设置和交叉编译

音视频技术开发周刊 | 251

You got 8K in the 3-year function test, but were overtaken by the new tester. In fact, you are pretending to work hard

> Could not create task ‘:app:MyTest. main()‘. > SourceSet with name ‘main‘ not found. Problem repair

Get 5 offers after being notified of layoffs

Le routage des microservices de la passerelle a échoué au chargement des ressources statiques des microservices

Opencv -- geometric space transformation (affine transformation and projection transformation)

Simple file transfer protocol TFTP

您的物联网安全性是否足够强大?
随机推荐
[issue 21] face to face experience of golang engineer recruited by Zhihu Society
被校园暴力,性格内向的马斯克凄惨而励志的童年
More, faster, better and cheaper. Here comes the fastdeploy beta of the low threshold AI deployment tool!
js清空对象和对象的值:
windows 2003 64位系统php运行报错:1% 不是有效的 win32 应用程序
基于流的深度生成模型
Agileplm exception resolution session
腾讯游戏发布40多款产品与项目 其中12款为新游戏
[today in history] May 31: the father of Amiga was born; The co developer of basic language was born; BlackBerry BBM shutdown
元宇宙标准论坛成立
LiveData 面试题库、解答---LiveData 面试 7 连问~
Reprinted article: the digital economy generates strong demand for computing power Intel releases a number of innovative technologies to tap the potential of computing power
How to run unity webgl after packaging (Firefox configuration)
PHP 代码 微信、公众号、企业微信 发送表情符号 [U+1F449]
Interview: how do lists duplicate objects according to their attributes?
树莓派-环境设置和交叉编译
CMU提出NLP新范式—重构预训练,高考英语交出134高分
RichView TRVStyle TextStyles
访问网站提示:您未被授权查看该页恢复办法
Mysql database operation - stored procedure, view, transaction, index, database backup