LeetCode 931. Minimum Falling Path Sum

Posted scotton-wild

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode 931. Minimum Falling Path Sum相关的知识,希望对你有一定的参考价值。

Given a square array of integers A, we want the minimum sum of a falling path through A.
A falling path starts at any element in the first row, and chooses one element from each row.  The next rows choice must be in a column that is different from the previous rows column by at most one.

求下降路径的最小和,反向DP,从下往上遍历

 1 class Solution {
 2 public:
 3     int minFallingPathSum(vector<vector<int>>& A) {
 4         int n=A.size();
 5         int dp[110][110]={0};
 6         for(int i=0; i<n; i++){
 7             dp[n-1][i]=A[n-1][i];
 8         }
 9         for(int i=n-2; i>=0; i--){
10             dp[i][0]=A[i][0]+min(dp[i+1][0],dp[i+1][1]);
11             dp[i][n-1]=A[i][n-1]+min(dp[i+1][n-1],dp[i+1][n-2]);
12             for(int j=1; j<n-1; j++){
13                 dp[i][j]=A[i][j]+min(dp[i+1][j-1], min(dp[i+1][j], dp[i+1][j+1]));
14             }
15         }
16         int ans=0x3f3f3f3f;
17         for(int i=0; i<n; i++){
18             ans=min(ans,dp[0][i]);
19         }
20         return ans;
21     }
22 };

 

以上是关于LeetCode 931. Minimum Falling Path Sum的主要内容,如果未能解决你的问题,请参考以下文章

931. Minimum Falling Path Sum

leetcode931

[Leetcode]931.下降路径最小和

leetcode 931. 下降路径最小和

Leetcode WC-108-03 931-下降路径最小和

leetcode刷题(125)——931. 下降路径最小和