当前位置:网站首页>LeetCode刷题--点滴记录021
LeetCode刷题--点滴记录021
2022-07-21 20:26:00 【鲁棒最小二乘支持向量机】
21. 剑指 Offer 47. 礼物的最大价值
要求
在一个 m*n 的棋盘的每一格都放有一个礼物,每个礼物都有一定的价值(价值大于 0)。你可以从棋盘的左上角开始拿格子里的礼物,并每次向右或者向下移动一格、直到到达棋盘的右下角。给定一个棋盘及其上面的礼物的价值,请计算你最多能拿到多少价值的礼物?
思路
动态规划:
- 状态定义: 设动态规划矩阵 dp ,dp(i,j) 代表从棋盘的左上角开始,到达单元格 (i,j) 时能拿到礼物的最大累计价值
- 当i=0且j=0时,为起始元素;当i=0且j≠0时,为矩阵第一行元素,只可从左边到达;当i≠0且j=0时,为矩阵第一列元素,只可从上边到达;当i≠0且j≠0时,可从左边或上边到达
- 初始状态: dp[0] = grid[0][0] ,即到达单元格 (0,0) 时能拿到礼物的最大累计价值为 grid[0][0]
- dp[m−1][n−1] ,m, n 分别为矩阵的行高和列宽,即返回 dp 矩阵右下角元素
解题
#include <iostream>
using namespace std;
#include <vector>
class Solution {
public:
int maxValue(vector<vector<int>>& grid) {
int m = grid.size(), n = grid[0].size(); // m行,n列
if (m == 1 && n == 1)
return grid[0][0];
vector<vector<int>> dp(m, vector<int>(n, 0)); // 二维数组定长初始化
dp[0][0] = grid[0][0]; // 起始元素
for (int i = 1; i < m; i++)
dp[i][0] = dp[i - 1][0] + grid[i][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++)
{
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];
}
};
void test01()
{
Solution solution;
vector<vector<int>>grid;
vector<int>grid1;
vector<int>grid2;
vector<int>grid3;
grid1.push_back(1);
grid1.push_back(3);
grid1.push_back(1);
grid2.push_back(1);
grid2.push_back(5);
grid2.push_back(1);
grid3.push_back(4);
grid3.push_back(2);
grid3.push_back(1);
grid.push_back(grid1);
grid.push_back(grid2);
grid.push_back(grid3);
cout << solution.maxValue(grid) << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
希望本文对大家有帮助,上文若有不妥之处,欢迎指正
分享决定高度,学习拉开差距
边栏推荐
- What is a video content recommendation engine?
- Value and technical thinking of vectorization engine for HTAP
- TZC 1283: 简单排序 —— 快速排序
- 100万人排队在等!DALL·E公开测试版,还收上费了
- unity2D 箭头动画
- systemd 管理 blackbox-exporter
- Matlab natural spline function (constraining the slope at both ends)
- 【公开课预告】:云视频会议系统私有化实践
- TypeScript—快速入门
- AcWing 1185. Word game solution (Euler circuit)
猜你喜欢
易基因 | 简化基因组DNA甲基化测序(RRBS)实验怎么做?
About BOM update of SAP apo rpmcall specified production order
《Multiple UAV exploration of an unknown region》翻译
"Messy and difficult" work tasks? This gadget will help you get it done easily!
sql语句,分组后,组内根据datetime字段排序,保留日期最新的一条组内记录,删除其他项。(用于组内重复数据的剔除)
Service worker guide-1
(PC+WAP)织梦模板情感资讯类网站
图解BERT、ELMo等 | NLP迁移学习开端
EN 1504-4:混凝土结构产品结构粘接—CE认证
B2B企业数字化转型,CIO如何避免踩坑
随机推荐
Matlab natural spline function (constraining the slope at both ends)
云呐|动环监控系统巡检,动环监控系统功能大致介绍
[unity3d] blood bar (HP)
美参议院初步通过520亿美元「芯片法案」,她竟乘机「投资炒股」!
《Multiple UAV exploration of an unknown region》翻译
GL_ TRIANGLE_ Fan and GL_ TRIANGLE_ STRIP
[string class]
Intelligent operation and maintenance scenario analysis: how to detect abnormal business system status through exception detection
Is the flush platform safe? Huatai Securities Account Opening app
Download datasets using kaggle's API
Robot modeling and 3D simulation based on ROS [physical / mechanical significance]
Pytoch training model fixes random seeds to ensure that the accuracy can be reproduced
Can you really use search engine?
Restful URL design specification
【Flutter -- 基础组件】单选开关(Switch)& 单选框(Radio) & 复选框(Checkbox)
你为什么会做测试/开发程序员?各路伙伴描述......
List分页方法
How can I sync previous online notes after OneNote is reinstalled or upgraded?
SYSTEMd management blackbox exporter
Dokcer running Nacos container automatic exit problem