当前位置:网站首页>剑指 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可视化
- Différences d'utilisation entre IsEmpty et isblank
- RichView TRVStyle TextStyles
- Initial linear regression
- Tips for visiting the website: you are not authorized to view the recovery method of this page
- Packet capturing and sorting out external Fiddler -- understanding the toolbar [1]
- Mixed programming of C language and assembly language in stm32
- [today in history] June 24: Netease was established; The first consumer electronics exhibition was held; The first webcast in the world
- STM32的C语言与汇编语言混合编程
- 嵌入式DSP音频开发
猜你喜欢

【插件-statistic】统计代码行数和相关数据

Intel Ruixuan A380 graphics card will be launched in China
![[today in history] June 18: JD was born; The online store platform Etsy was established; Facebook releases Libra white paper](/img/88/6cdd2b604522261e2a88020c5d6ae7.jpg)
[today in history] June 18: JD was born; The online store platform Etsy was established; Facebook releases Libra white paper

【Kotlin】在Android官方文档中对其语法的基本介绍和理解

第一次使用gcc和makefile编写c程序

简单ELK配置实现生产级别的日志采集和查询实践

【活动早知道】LiveVideoStack近期活动一览
![[today in history] June 10: Apple II came out; Microsoft acquires gecad; The scientific and technological pioneer who invented the word](/img/0d/9f99eb3dcb73c912987b81fba71890.png)
[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

CMU提出NLP新范式—重构预训练,高考英语交出134高分

Mixed programming of C language and assembly language in stm32
随机推荐
为什么大厂压力大,竞争大,还有这么多人热衷于大厂呢?
您的物联网安全性是否足够强大?
2021年软件测试工具总结——模糊测试工具
Mysql database operation - stored procedure, view, transaction, index, database backup
[522. longest special sequence II]
Review the submission of small papers for 2022 spring semester courses
[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
【活动早知道】LiveVideoStack近期活动一览
Apache——阿帕奇简介
Which securities platform is the best and safest for a novice to open a stock trading account
Online JSON to plaintext tool
一位博士在华为的22年(干货满满)
[today in history] June 11: the co inventor of Monte Carlo method was born; Google launched Google Earth; Google acquires waze
Simple file transfer protocol TFTP
嵌入式DSP音频开发
R语言惩罚逻辑回归、线性判别分析LDA、广义加性模型GAM、多元自适应回归样条MARS、KNN、二次判别分析QDA、决策树、随机森林、支持向量机SVM分类优质劣质葡萄酒十折交叉验证和ROC可视化
[today in history] June 20: the father of MP3 was born; Fujitsu was established; Google acquires dropcam
Gateway微服務路由使微服務靜態資源加載失敗
Summary of software testing tools in 2021 - fuzzy testing tools
初始线性回归