当前位置:网站首页>966 minimum path sum
966 minimum path sum
2022-07-06 21:00:00 【-Lin Zeyu】
The title is as follows
Given a... That contains a nonnegative integer m x n grid grid , Please find a path from the top left corner to the bottom right corner , Make the sum of the numbers on the path the smallest .
explain : You can only move down or right one step at a time .
Example 1:
Input :grid = [[1,3,1],[1,5,1],[4,2,1]]
Output :7
explain : Because the path 1→3→1→1→1 The sum of is the smallest .
Example 2:
Input :grid = [[1,2,3],[4,5,6]]
Output :12
m = = grid.length
n = = grid[i].length
1 <= m, n <= 200
0 <= grid[i][j] <= 100
Their thinking
Dynamic programming 
Solution code
class Solution
{
public:
int minPathSum(vector<vector<int>>& grid)
{
int m = grid.size(), n = grid[0].size(), dp[m][n], i, j;
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
if (i == 0 && j == 0)
{
dp[0][0] = grid[0][0];
}
else if (i == 0 && j != 0)
{
dp[i][j] = dp[i][j - 1] + grid[i][j];
}
else if (j == 0 && i != 0)
{
dp[i][j] = dp[i - 1][j] + grid[i][j];
}
else
{
dp[i][j] = std::min(dp[i - 1][j], dp[i][j - 1]) + grid[i][j];
}
}
}
return dp[m - 1][n - 1];
}
};
边栏推荐
- C language games - minesweeping
- 2022 Guangdong Provincial Safety Officer C certificate third batch (full-time safety production management personnel) simulation examination and Guangdong Provincial Safety Officer C certificate third
- Application layer of tcp/ip protocol cluster
- How to turn a multi digit number into a digital list
- 1_ Introduction to go language
- Introduction to the use of SAP Fiori application index tool and SAP Fiori tools
- Mécanisme de fonctionnement et de mise à jour de [Widget Wechat]
- Can novices speculate in stocks for 200 yuan? Is the securities account given by qiniu safe?
- OLED屏幕的使用
- User defined current limiting annotation
猜你喜欢
![[DSP] [Part 1] start DSP learning](/img/81/051059958dfb050cb04b8116d3d2a8.png)
[DSP] [Part 1] start DSP learning

Reinforcement learning - learning notes 5 | alphago

Why do novices often fail to answer questions in the programming community, and even get ridiculed?

The most comprehensive new database in the whole network, multidimensional table platform inventory note, flowus, airtable, seatable, Vig table Vika, flying Book Multidimensional table, heipayun, Zhix
![[DSP] [Part 2] understand c6678 and create project](/img/06/54b1cf1f5b3308fffb4f84dcf7db9b.png)
[DSP] [Part 2] understand c6678 and create project

基于STM32单片机设计的红外测温仪(带人脸检测)

【mysql】游标的基本使用

(工作记录)2020年3月11日至2021年3月15日

No Yum source to install SPuG monitoring

性能测试过程和计划
随机推荐
Data Lake (VIII): Iceberg data storage format
【mysql】游标的基本使用
APS taps home appliance industry into new growth points
Notes - detailed steps of training, testing and verification of yolo-v4-tiny source code
新型数据库、多维表格平台盘点 Notion、FlowUs、Airtable、SeaTable、维格表 Vika、飞书多维表格、黑帕云、织信 Informat、语雀
[weekly pit] output triangle
Statistical inference: maximum likelihood estimation, Bayesian estimation and variance deviation decomposition
【mysql】触发器
[diy] how to make a personalized radio
基于深度学习的参考帧生成
PHP saves session data to MySQL database
Redis insert data garbled solution
Spark SQL chasing Wife Series (initial understanding)
[DIY]自己设计微软MakeCode街机,官方开源软硬件
3D face reconstruction: from basic knowledge to recognition / reconstruction methods!
Use of OLED screen
(work record) March 11, 2020 to March 15, 2021
监控界的最强王者,没有之一!
数据湖(八):Iceberg数据存储格式
面试官:Redis中有序集合的内部实现方式是什么?