当前位置:网站首页>Sword finger offer series - 47 Maximum value of gifts
Sword finger offer series - 47 Maximum value of gifts
2022-06-12 22:33:00 【Listen to the sea】
subject
In a m*n There is a gift in every space of the chessboard , Every gift has a certain value ( Value greater than 0). You can start from the top left corner of the chessboard to get the gifts in the grid , And move right or down one space at a time 、 Until you reach the bottom right corner of the chessboard . Given the value of a chessboard and its gifts , Please calculate the maximum value of gifts you can get ?
Example
Input :
[
[1,3,1],
[1,5,1],
[4,2,1]
]
Output : 12
explain : route 1→3→5→2→1 Can get the most value gifts
Ideas
We can only go right or down one grid at a time , So the first line 、 There is only one way to go in the first column , You can open a two-dimensional array and record the first line first 、 The first column is the gift value that each grid can get , Then calculate the of the remaining lattice . Each grid should be equal to the grid above it and the grid on the left. Take a maximum value plus the value on the grid of the original array . Finally, the newly calculated value in the lower right corner is the result we want
Code
class Solution {
public:
int maxValue(vector<vector<int>>& grid) {
int m = grid.size(), n = grid[0].size();
vector<vector<int>> dp(m, vector<int>(n));
dp[0][0] = grid[0][0];
for (int i = 1; i < grid[0].size(); i ++ ){
dp[0][i] = dp[0][i - 1] + grid[0][i];
}
for (int i = 1; i < grid.size(); i ++ ){
dp[i][0] = dp[i - 1][0] + grid[i][0];
}
for (int i = 1; i < m; i ++ ) {
for (int j = 1; j < n; j ++ ) {
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1])+grid[i][j];
}
}
return dp[m - 1][n - 1];
}
};
边栏推荐
- 同花顺股票账户开户安全吗
- VIM use the lower right 4 keys
- 年薪50万是一条线,年薪100万又是一条线…...
- China barcode decoder market trend report, technical innovation and market forecast
- Photoshop:ps how to enlarge a picture without blurring
- Research Report on water sports shoes industry - market status analysis and development prospect forecast
- JVM Basics - > What are the JVM parameters?
- 3.5 setup and teardown of test classes
- Generate the chrysanthemum code of the applet (generate the chrysanthemum code, change the middle logo, change the image size, and add text)
- Redis optimization
猜你喜欢
![[image denoising] image denoising based on trilateral filter with matlab code](/img/f2/770a0e2938728e731c18c0a66f7c12.png)
[image denoising] image denoising based on trilateral filter with matlab code

Anti aliasing / anti aliasing Technology

Mr. Sun's version of JDBC (21:34:25, June 12, 2022)

Configuring Dingding notification of SQL audit platform archery

Kotlin collaboration process - flow

flutter系列之:flutter中常用的GridView layout详解

Web3 principle and decentralization

接口测试工具apipost3.0版本对于流程测试和引用参数变量

Inventory of CV neural network models from 2021 to 2022

The shutter library recommends sizer to help you easily create a responsive UI
随机推荐
A 42 year old senior executive of a large factory reminds people aged 30-39 that these six habits that make you stronger should be developed as soon as possible
QT quick 3D learning: mouse picking up objects
[sword finger offer simple] sword finger offer 24 Reverse linked list
【LeetCode】53.最大子数组和
42岁大厂高管,给30岁-39岁人提个醒:这6个让你变强的习惯,要尽快养成
数据库每日一题---第10天:组合两个表
Leetcode: the maximum number of building change requests that can be reached (if you see the amount of data, you should be mindless)
Several Tsinghua students I know have left
Zabbix的功能介绍和常用术语
C#读取word中表格数据
【LeetCode】53. Maximum subarray and
Flutter库推荐Sizer 可帮助您轻松创建响应式 UI
Anti aliasing / anti aliasing Technology
LNMP platform docking redis service
Research and Analysis on the development of China's Melamine Industry from 2022 to 2028 and market prospect forecast report
Implementation of master-slave replication and master-master replication for MySQL and MariaDB databases
打新债开户安全么,新手该怎么操作?
认识的几位清华同学都离职了……
【LeetCode】5. Longest Palindromic Substring
Web3 principle and decentralization