[Learning Dynamic Programming] The minimum sum of the descending path (8)

Table of contents

How to learn dynamic programming?

1. Topic analysis

2. Algorithm principle

1. Status representation

2. State transition equation

3. Initialization

4. Filling order

5. Return value

3. Code writing

Write at the end:


How to learn dynamic programming?

There is no shortcut to learning an algorithm, let alone learning dynamic programming,

Brush dynamic programming algorithm questions with me, and learn dynamic programming together!

1. Topic analysis

Topic link: 931. Minimum sum of descending paths - Leetcode

 This question is very difficult to read, but it can be well understood by drawing a picture:

Let's say we start with the square pointed by the arrow:

The path he can go down is i in the three grids in the picture.

Find the minimum sum of paths and return.

2. Algorithm principle

1. Status representation

What does dp[ i ][ j ] represent?

dp[ i ][ j ] represents the minimum value of the path when reaching [ i, j ].

2. State transition equation

Find from the most recent step, and the most recent step has three situations:

1. From top left: dp[ i - 1 ][ j - 1 ] + m[ i ][ j ]

2. From above: dp[ i - 1 ][ j ] + m[ i ][ j ]

3. From top right: dp[ i - 1 ][ j + 1 ] + m[ i ][ j ]

Because what we want is the smallest path sum, the state transition equation is:

dp[ i ][ j ] = min( dp[ i - 1 ][ j - 1 ],dp[ i - 1 ][ j ],dp[ i - 1 ][ j + 1 ] ) + m[ i ][ j ]     

3. Initialization

In order to prevent crossing the border, we have to add a line to the leftmost, topmost and rightmost.

However, the two rows on the left and right cannot be initialized to 0, and must be initialized to a large number to prevent the calculation from being affected.

So we first turn all positions into very large numbers, and then change the first line to 0.

4. Filling order

From top to bottom, from left to right (this is indifferent (because the left and right values ​​are not used))

5. Return value

Returns the minimum value of the last row.

3. Code writing

class Solution {
public:
    int minFallingPathSum(vector<vector<int>>& matrix) {
        int m = matrix.size(), n = matrix[0].size(); 
        vector<vector<int>> dp(m + 1, vector<int>(n + 2, INT_MAX));
        for(auto& e : dp[0]) e = 0;
        for(int i = 1; i <= m; i++) {
            for(int j = 1; j <= n; j++) {
                dp[i][j] = min(dp[i - 1][j - 1], min(dp[i - 1][j], dp[i - 1][j + 1]))
                + matrix[i - 1][j - 1];
            }
        }
        int ans = INT_MAX;
        for(const auto& k : dp[m]) ans = min(ans, k);
        return ans; 
    }
};

Write at the end:

The above is the content of this article, thank you for reading.

If you feel that you have gained something, you can give the blogger a like .

If there are omissions or mistakes in the content of the article, please private message the blogger or point it out in the comment area~

Guess you like

Origin blog.csdn.net/Locky136/article/details/131633477
Recommended