当前位置:网站首页>leetcode 931. Minimum sum of descent path
leetcode 931. Minimum sum of descent path
2022-07-22 06:11:00 【Code of attack】
931. The descent path is the smallest and
List of articles
One 、 subject
Original link :931. The descent path is the smallest and
1. Title Description
To give you one n x n
Of square An array of integers matrix
, Please find out and return through matrix
Of descent path Of Minimum and .
descent path You can start with any element in the first line , And select an element from each row . The element selected in the next row is at most one column apart from the element selected in the current row ( That is, the first element directly below or diagonally left or right ). say concretely , Location (row, col)
The next element of should be (row + 1, col - 1)
、(row + 1, col)
perhaps (row + 1, col + 1)
.
Example 1:

Input :matrix = [[2,1,3],[6,5,4],[7,8,9]]
Output :13
explain : As shown in the figure , And the smallest two descent paths
Example 2:

Input :matrix = [[-19,57],[-40,-5]]
Output :-59
explain : As shown in the figure , Is the minimum descent path
Tips :
n == matrix.length == matrix[i].length
1 <= n <= 100
-100 <= matrix[i][j] <= 100
2. Basic framework
C++ The basic framework code is as follows :
int minFallingPathSum(vector<vector<int>>& matrix) {
}
3. Their thinking
- Topic analysis
The goal of the problem is to find the minimum sum of the descent path , It belongs to the minimum value problem .
From
i
Any element in the row can only be summed in the downward direction .(i,j)
The square can drop to(i,j - 1)
、(i, j)
or(i,j + 1)
Of diamonds .It can be concluded that the recurrence formula is :
martrix[i][j] += min{martrix[i - 1][j - 1], martrix[i - 1][j], martrix[i - 1][j + 1]}
When
j = 0
Orj = matrix.size() - 1
when , It needs to be dealt with separately :j = 0
, Only from the upper layerj
andj + 1
To come over .j = matrix.size() - 1
, Only from the upper layerj - 1
andj
To come over .- If only exist
j = 1
Of matrix Array , namelyj = matrix.size() - 1 = 0
, Then only from the upper layerj
Come down .
Sort the last row once , Choose the smallest path and .
Implementation code :
int minFallingPathSum(vector<vector<int>>& matrix) { int i, j; for (i = 1; i < matrix.size(); i++) { for (j = 0; j < matrix[i].size(); j++) { if (j == 0 && j == matrix[i].size() - 1) matrix[i][j] += matrix[i - 1][j]; else if (j == 0) matrix[i][j] += min(matrix[i - 1][j], matrix[i - 1][j + 1]); else if (j == matrix[i].size() - 1) matrix[i][j] += min(matrix[i - 1][j - 1], matrix[i - 1][j]); else matrix[i][j] += min(matrix[i - 1][j - 1], min(matrix[i - 1][j], matrix[i - 1][j + 1])); } } sort(matrix[i - 1].begin(), matrix[i - 1].end()); return matrix[i - 1][0]; }
边栏推荐
- 百度飞桨EasyDL X 韦士肯:看轴承质检如何装上“AI之眼”
- Control of semiconductor refrigeration and heating based on general single chip microcomputer control of cold and hot head in mobile phone radiator massage instrument
- 数据中心运维管理技能的重要性
- 申万宏源证券股票低佣金开户靠谱吗,可靠安全吗
- Detr code
- 6.1 恶意代码防范
- 问题来了:4GB物理内存的机器上申请8G内存能成功吗?
- leetcode 1732.找到最高海拔
- 学习作业:
- IP协议,卫冕之王
猜你喜欢
随机推荐
爬虫与反爬:一场无休止之战
FPGA设计中遇到的奇葩问题之“芯片也要看出身”(三)
Discussion on passing D shield JSP webshell+ ice scorpion avoiding killing horses
How to realize visualization of basic equipment efficiently
ETH POS 2.0 Staking 测试网质押流程
Discussion on ASP webshell+ ice scorpion free horse killing
leetcode 1413.逐步求和得到正数的最小值
A device from black to white (with front desk 0day)
DRDS 柔性事务漫谈
Goldfish rhca memoirs: cl210 performs image operation -- compare image format + build custom image
剑指 Offer II 100. 三角形中最小路径之和
The pit trodden by real people tells you to avoid the 10 mistakes that novices in automated testing often make
国产统信UOS系统运行小程序的探索
Leetcode 539. 最小时间差
开户做期货那家公司手续费低 安全
剑指 Offer II 015. 字符串中的所有变位词
学习作业:
METRONIC Management Dashboard, advanced guidance dashboard theme
C language problem solving number sequence
leetcode 724.寻找数组的中心下标