当前位置:网站首页>Sword finger offer II 099 Sum of minimum paths
Sword finger offer II 099 Sum of minimum paths
2022-07-22 06:11:00 【Code of attack】
The finger of the sword Offer II 099. Sum of minimum paths
List of articles
One 、 subject
Original link : The finger of the sword Offer II 099. Sum of minimum paths
1. Title Description
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 : A robot 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 :12Tips :
m == grid.length
n == grid[i].length
1 <= m, n <= 200
0 <= grid[i][j] <= 100
2. Basic framework
C++ The basic framework code is as follows :
int minPathSum(vector<vector<int> >& grid){
}
3. Their thinking
- Topic analysis
- The goal of the topic is to return the minimum sum of numbers on the path from the upper left corner to the lower right corner .
- This problem is the problem of finding the best value , Dynamic programming can be considered .
- except 0 That's ok 0 Outside the column
dp[i][j]
May bydp[i - 1][j]
ordp[i][j - 1]
Turn around , So the recursive formula we can get is :dp[i][j] = min(dp[i - 1][j], dp[i][j - 1]) + grid[i][j];
dp[i][j]
Express i,j The sum of the minimum paths taken by the current position .- because
dp[i][j]
State bydp[i - 1][j] and dp[i][j - 1]
Transfer from , We need to i by 0 and j by 0 These two cases are initialized . i = 0
When , It means there is only one path , thereforei = 0
Each column in this row is initialized as the numerical cumulative sum of the current path from left to right ,j = 0
Each row in this column should also be initialized as the numerical sum of the path from top to bottom .
Implementation code :
int minPathSum(vector<vector<int>>& grid) { int m = grid.size(); int n = grid[0].size(); vector<vector<int> > dp(m, vector<int>(n)); dp[0][0] = grid[0][0]; // initialization (0,0) Minimum value of position for (int i = 1; i < m; i++) // Initializing page 0 Column Division (0,0) Path and minimum value of position dp[i][0] = grid[i][0] + dp[i - 1][0]; for (int j = 1; j < n; j++) // // Initializing page 0 Row Division (0,0) Path and minimum value of position dp[0][j] = grid[0][j] + dp[0][j - 1]; for (int i = 1; i < m; i++) for (int j = 1; j < n; j++) dp[i][j] = min(dp[i - 1][j], dp[i][j - 1]) + grid[i][j]; return dp[m - 1][n - 1]; }
边栏推荐
- The wonderful problem encountered in FPGA design - "chip also depends on origin" (III)
- Discussion on passing D shield JSP webshell+ ice scorpion avoiding killing horses
- C#. Net sqlserver login function
- [book club issue 13] + Chapter III coding format of video files
- The difference between w3school and w3school
- DETR代码
- Early details about visual studio 2019
- Idea error port 8080 is already in use
- [book club issue 13] + Chapter 1 multimedia processing tool ffmpeg tool set
- Internet and transport layer protocol
猜你喜欢
随机推荐
Noipd2t2 - treasure solution
互联网和传输层协议
leetcode 1582. 二进制矩阵中的特殊位置
Haproxy2.6 load installation configuration
Tencent took out 38K two days ago, which showed me the basic ceiling. Today share is for you~
ssm框架文件上传已经实现怎么映射到数据库
【读书会第13期】+第三章 视频文件的编码格式
微信小程序_19,自定义组件-behaviors
[book club issue 13] + Chapter 1 multimedia processing tool ffmpeg tool set
supervisor常用命令
C#多线程和异步(二)——Task和async/await详解
Idea error port 8080 is already in use
vector容器成员函数——reserve()及迭代器失效问题
Wechat applet_ 19. Custom components -behaviors
移动研发平台EMAS 3.0全新升级,欢迎登陆阿里云官网搜索EMAS进行体验
Discussion on DLL killing free technology
Discussion on passing the d-shield PHP webshell without killing horses
移动机器人(四)四轴飞行器
问题来了:4GB物理内存的机器上申请8G内存能成功吗?
Talk about 36 tips of interface design