下降路径最小和

*************

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矩阵来举例。

matrixdp
133438133438
1234
5678
133438

                                        

 简单来说,从第2行开始,dp[1][j] 单元格就是min(dp[1][j - 1], dp[1][j], dp[1][j + 1]),当然,这三项有时候不可能同时存在,比如说dp[1][0]的左上角就不存在这个数,不存在的数字可以用无穷大表示。所以在进行填充的时候,需要找到当前单元格正上方3个单元格中的每个数字。此处为了提高代码的易读性,需要对这3个单元格取个好记的名字。此处使用dp[1][0]举例子。 

matrixdp
133438133438
12341
5678
133438

这里牵扯到一个专业的语法,就是 首先判断condition是否为真,如果是,就赋值value,如果不是,就赋值10086;

matrixdp
13343810086133438
12341
5678
133438

 

 

 

 

 

 

继续遍历,就会发现另外取名字的好处了,非常的优雅。

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);
            }
        }
    }
};
matrixdp
1334381008613343810086
1234100861 +  32 + 33 + 34 + 410086
56781008610086
133438

 

 

 

 

 

最后,只需要 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;
    }
};

完结,撒花,周末愉快!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值