当前位置:网站首页>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];
}
};
边栏推荐
- Is it safe to open an account in flush? Which securities company is good at opening an account? Low handling charges
- Use of OLED screen
- HMS Core 机器学习服务打造同传翻译新“声”态,AI让国际交流更顺畅
- Variable star --- article module (1)
- (工作记录)2020年3月11日至2021年3月15日
- Simple continuous viewing PTA
- New database, multidimensional table platform inventory note, flowus, airtable, seatable, Vig table Vika, Feishu multidimensional table, heipayun, Zhixin information, YuQue
- Common English vocabulary that every programmer must master (recommended Collection)
- Detailed explanation of knowledge map construction process steps
- How to implement common frameworks
猜你喜欢
![Mécanisme de fonctionnement et de mise à jour de [Widget Wechat]](/img/cf/58a62a7134ff5e9f8d2f91aa24c7ac.png)
Mécanisme de fonctionnement et de mise à jour de [Widget Wechat]

OLED屏幕的使用

New database, multidimensional table platform inventory note, flowus, airtable, seatable, Vig table Vika, Feishu multidimensional table, heipayun, Zhixin information, YuQue

【微信小程序】运行机制和更新机制

PHP online examination system version 4.0 source code computer + mobile terminal

【OpenCV 例程200篇】220.对图像进行马赛克处理

Intel 48 core new Xeon run point exposure: unexpected results against AMD zen3 in 3D cache

全网最全的知识库管理工具综合评测和推荐:FlowUs、Baklib、简道云、ONES Wiki 、PingCode、Seed、MeBox、亿方云、智米云、搜阅云、天翎

审稿人dis整个研究方向已经不仅仅是在审我的稿子了怎么办?

15million employees are easy to manage, and the cloud native database gaussdb makes HR office more efficient
随机推荐
【微信小程序】运行机制和更新机制
Hardware development notes (10): basic process of hardware development, making a USB to RS232 module (9): create ch340g/max232 package library sop-16 and associate principle primitive devices
[MySQL] trigger
过程化sql在定义变量上与c语言中的变量定义有什么区别
Application layer of tcp/ip protocol cluster
Detailed explanation of knowledge map construction process steps
Performance test process and plan
Le langage r visualise les relations entre plus de deux variables de classification (catégories), crée des plots Mosaiques en utilisant la fonction Mosaic dans le paquet VCD, et visualise les relation
Common doubts about the introduction of APS by enterprises
Why do novices often fail to answer questions in the programming community, and even get ridiculed?
7、数据权限注解
Introduction to the use of SAP Fiori application index tool and SAP Fiori tools
使用.Net分析.Net达人挑战赛参与情况
Web开发小妙招:巧用ThreadLocal规避层层传值
New database, multidimensional table platform inventory note, flowus, airtable, seatable, Vig table Vika, Feishu multidimensional table, heipayun, Zhixin information, YuQue
(work record) March 11, 2020 to March 15, 2021
Intel 48 core new Xeon run point exposure: unexpected results against AMD zen3 in 3D cache
OneNote 深度评测:使用资源、插件、模版
基于深度学习的参考帧生成
None of the strongest kings in the monitoring industry!