当前位置:网站首页>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];
}
};
边栏推荐
- OLED屏幕的使用
- Implementation of packaging video into MP4 format and storing it in TF Card
- ##无yum源安装spug监控
- 【mysql】游标的基本使用
- Intel 48 core new Xeon run point exposure: unexpected results against AMD zen3 in 3D cache
- 拼多多败诉,砍价始终差0.9%一案宣判;微信内测同一手机号可注册两个账号功能;2022年度菲尔兹奖公布|极客头条
- Build your own application based on Google's open source tensorflow object detection API video object recognition system (IV)
- #yyds干货盘点#重新梳理箭头函数的this
- 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
- 【微信小程序】运行机制和更新机制
猜你喜欢
KDD 2022 | 通过知识增强的提示学习实现统一的对话式推荐
Spark SQL chasing Wife Series (initial understanding)
##无yum源安装spug监控
Reference frame generation based on deep learning
小孩子學什麼編程?
Deployment of external server area and dual machine hot standby of firewall Foundation
Implementation of packaging video into MP4 format and storing it in TF Card
Application layer of tcp/ip protocol cluster
OAI 5g nr+usrp b210 installation and construction
2022 nurse (primary) examination questions and new nurse (primary) examination questions
随机推荐
2022 portal crane driver registration examination and portal crane driver examination materials
Pinduoduo lost the lawsuit, and the case of bargain price difference of 0.9% was sentenced; Wechat internal test, the same mobile phone number can register two account functions; 2022 fields Awards an
[diy] how to make a personalized radio
OAI 5g nr+usrp b210 installation and construction
Select data Column subset in table R [duplicate] - select subset of columns in data table R [duplicate]
Intel 48 core new Xeon run point exposure: unexpected results against AMD zen3 in 3D cache
Spark SQL chasing Wife Series (initial understanding)
OneNote 深度评测:使用资源、插件、模版
Notes - detailed steps of training, testing and verification of yolo-v4-tiny source code
Build your own application based on Google's open source tensorflow object detection API video object recognition system (IV)
3D face reconstruction: from basic knowledge to recognition / reconstruction methods!
Review questions of anatomy and physiology · VIII blood system
Utilisation de l'écran OLED
正则表达式收集
[MySQL] basic use of cursor
'class file has wrong version 52.0, should be 50.0' - class file has wrong version 52.0, should be 50.0
Swagger UI教程 API 文档神器
What are RDB and AOF
OAI 5G NR+USRP B210安装搭建
Common doubts about the introduction of APS by enterprises