*************
C++
Leetcode: 931. 下降路径最小和 - 力扣(LeetCode)
*************

看到题目,感觉跟 三角形最小路径和 很像,因此决定还是用 dp 解决问题,我喜欢 dp
矩阵,因为目前我只会 dp 矩阵。
起手就是获得矩阵的行列数和初始化一个简单的 dp 矩阵。
class Solution {
public:
int minFallingPathSum(vector<vector<int>>& matrix) {
int m = matrix.size(); // get the rows
int n = matrix[0].size(); // get the columns
vector<vector<int>> dp(m, vector<int>(n, 0)) // initialise the arrary
}
};
其次就是 dp 矩阵的第一行和 matrix 的第一行是一致的,因此简单的填充 dp 矩阵的第一行。
class Solution {
public:
int minFallingPathSum(vector<vector<int>>& matrix) {
int m = matrix.size(); // get the rows
int n = matrix[0].size(); // get the columns
vector<vector<int>> dp(m, vector<int>(n, 0)) // initialise the arrary
for (int j = 0; j < n; i++){
dp[0][j] = matrix[0][j]; // fill the first row
}
}
};
接下来的思路就是依次填充剩下的行数。为了帮助自己更好的理解,使用一个简单的4 × 4矩阵来举例。
| matrix | dp | ||||||||
| 13 | 3 | 4 | 38 | 13 | 3 | 4 | 38 | ||
| 1 | 2 | 3 | 4 | ||||||
| 5 | 6 | 7 | 8 | ||||||
| 13 | 3 | 4 | 38 |
简单来说,从第2行开始,dp[1][j] 单元格就是min(dp[1][j - 1], dp[1][j], dp[1][j + 1]),当然,这三项有时候不可能同时存在,比如说dp[1][0]的左上角就不存在这个数,不存在的数字可以用无穷大表示。所以在进行填充的时候,需要找到当前单元格正上方3个单元格中的每个数字。此处为了提高代码的易读性,需要对这3个单元格取个好记的名字。此处使用dp[1][0]举例子。
| matrix | dp | ||||||||
| 13 | 3 | 4 | 38 | 13 | 3 | 4 | 38 | ||
| 1 | 2 | 3 | 4 | 1 | |||||
| 5 | 6 | 7 | 8 | ||||||
| 13 | 3 | 4 | 38 |
这里牵扯到一个专业的语法,就是 首先判断condition是否为真,如果是,就赋值value,如果不是,就赋值10086;
| matrix | dp | ||||||||
| 13 | 3 | 4 | 38 | 10086 | 13 | 3 | 4 | 38 | |
| 1 | 2 | 3 | 4 | 1 | |||||
| 5 | 6 | 7 | 8 | ||||||
| 13 | 3 | 4 | 38 |
继续遍历,就会发现另外取名字的好处了,非常的优雅。
condition ? value : 10086
class Solution {
public:
int minFallingPathSum(vector<vector<int>>& matrix) {
int m = matrix.size(); // get the rows
int n = matrix[0].size(); // get the columns
vector<vector<int>> dp(m, vector<int>(n, 0)) // initialise the arrary
for (int j = 0; j < n; i++){
dp[0][j] = matrix[0][j]; // fill the first row
}
for (inti = 1; i < m; i++){ // start from the second row
for (int j = 0; j < n; j++){ // start from the first column
int above = dp[i - 1][j];
int diagLeft = j > 0 ? dp[i - 1][j - 1] : INT_MAX;
int diagRight = j < n - 1 ? dp[i - 1][j + 1] : INT_MAX;
// fill the dp array
dp[i][j] = matrix[i][j] + min(above, diagLeft, diagRight);
}
}
}
};
| matrix | dp | |||||||||
| 13 | 3 | 4 | 38 | 10086 | 13 | 3 | 4 | 38 | 10086 | |
| 1 | 2 | 3 | 4 | 10086 | 1 + 3 | 2 + 3 | 3 + 3 | 4 + 4 | 10086 | |
| 5 | 6 | 7 | 8 | 10086 | 10086 | |||||
| 13 | 3 | 4 | 38 |
最后,只需要 dp 矩阵的最后一行踪到最小值就OK了。
class Solution {
public:
int minFallingPathSum(vector<vector<int>>& matrix) {
int m = matrix.size(); // get the rows
int n = matrix[0].size(); // get the columns
vector<vector<int>> dp(m, vector<int>(n, 0)) // initialise the arrary
for (int j = 0; j < n; i++){
dp[0][j] = matrix[0][j]; // fill the first row
}
for (inti = 1; i < m; i++){ // start from the second row
for (int j = 0; j < n; j++){ // start from the first column
int above = dp[i - 1][j];
int diagLeft = j > 0 ? dp[i - 1][j - 1] : INT_MAX;
int diagRight = j < n - 1 ? dp[i - 1][j + 1] : INT_MAX;
// fill the dp array
dp[i][j] = matrix[i][j] + min(above, diagLeft, diagRight);
}
}
// find the minimum eliment in the last row
int minSum = INT_MAX;
for (int j = 0; j < n; j++){
minSum = min(minSum, dp[n - 1][j]);
}
return minSum;
}
};
但其实,运行这段代码的时候,根本跑不起来,还总是报错,是因为之前的代码有几处错误,根据leetcode的提示更改就OK了。
比如说,
第7行的最后没有 ;
第9行应该是 j++
最后,细心一点,完整代码就跑的通了。
class Solution {
public:
int minFallingPathSum(vector<vector<int>>& matrix) {
int m = matrix.size(); // get the rows
int n = matrix[0].size(); // get the columns
vector<vector<int>> dp(m, vector<int>(n, 0)); // initialise the arrary
for (int j = 0; j < n; j++){
dp[0][j] = matrix[0][j]; // fill the first row
}
for (int i = 1; i < m; i++){ // start from the second row
for (int j = 0; j < n; j++){ // start from the first column
int above = dp[i - 1][j];
int diagLeft = j > 0 ? dp[i - 1][j - 1] : INT_MAX;
int diagRight = j < n - 1 ? dp[i - 1][j + 1] : INT_MAX;
// fill the dp array
dp[i][j] = matrix[i][j] + min({above, diagLeft, diagRight});
}
}
// find the minimum eliment in the last row
int minSum = INT_MAX;
for (int j = 0; j < n; j++){
minSum = min(minSum, dp[n - 1][j]);
}
return minSum;
}
};
完结,撒花,周末愉快!


被折叠的 条评论
为什么被折叠?



