当前位置:网站首页>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];
}
};
边栏推荐
- KDD 2022 | 通过知识增强的提示学习实现统一的对话式推荐
- PHP online examination system version 4.0 source code computer + mobile terminal
- 2110 summary of knowledge points and common problems in redis class
- 【OpenCV 例程200篇】220.对图像进行马赛克处理
- PHP saves session data to MySQL database
- R language visualizes the relationship between more than two classification (category) variables, uses mosaic function in VCD package to create mosaic plots, and visualizes the relationship between tw
- Minimum cut edge set of undirected graph
- Utilisation de l'écran OLED
- OLED屏幕的使用
- User defined current limiting annotation
猜你喜欢
[diy] self designed Microsoft makecode arcade, official open source software and hardware
Swagger UI教程 API 文档神器
Reinforcement learning - learning notes 5 | alphago
Distributed ID
Core principles of video games
Reviewer dis's whole research direction is not just reviewing my manuscript. What should I do?
Deployment of external server area and dual machine hot standby of firewall Foundation
Opencv learning example code 3.2.3 image binarization
[DIY]如何制作一款个性的收音机
硬件开发笔记(十): 硬件开发基本流程,制作一个USB转RS232的模块(九):创建CH340G/MAX232封装库sop-16并关联原理图元器件
随机推荐
1500万员工轻松管理,云原生数据库GaussDB让HR办公更高效
(work record) March 11, 2020 to March 15, 2021
C language games - three chess
OAI 5G NR+USRP B210安装搭建
Activiti global process monitors activitieventlistener to monitor different types of events, which is very convenient without configuring task monitoring in acitivit
Manifest of SAP ui5 framework json
Regular expression collection
How to implement common frameworks
Comprehensive evaluation and recommendation of the most comprehensive knowledge base management tools in the whole network: flowus, baklib, jiandaoyun, ones wiki, pingcode, seed, mebox, Yifang cloud,
APS taps home appliance industry into new growth points
OneNote 深度评测:使用资源、插件、模版
Pycharm remote execution
Xcode6 error: "no matching provisioning profiles found for application"
Notes - detailed steps of training, testing and verification of yolo-v4-tiny source code
Infrared thermometer based on STM32 single chip microcomputer (with face detection)
Leetcode hot topic Hot 100 day 32: "minimum coverage substring"
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
请问sql group by 语句问题
面试官:Redis中有序集合的内部实现方式是什么?
Introduction to the use of SAP Fiori application index tool and SAP Fiori tools