Created
December 18, 2018 10:32
-
-
Save fpdjsns/18f4d32c49ac294a4175065fc8cfca90 to your computer and use it in GitHub Desktop.
[leetcode] 931. Minimum Falling Path Sum : https://leetcode.com/problems/minimum-falling-path-sum/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public: | |
int minFallingPathSum(vector<vector<int>>& A) { | |
int N = A.size(); | |
vector<vector<int>> d(N, vector<int>(N, 1e9)); | |
for(int i=0; i<N; i++) d[0][i] = A[0][i]; | |
for(int i=0; i<N-1;i++){ | |
for(int j=0; j<N; j++){ | |
if(0<j) d[i+1][j-1] = min(A[i+1][j-1] + d[i][j], d[i+1][j-1]); | |
d[i+1][j] = min(A[i+1][j] + d[i][j], d[i+1][j]); | |
if(j<N-1) d[i+1][j+1] = min(A[i+1][j+1] + d[i][j], d[i+1][j+1]); | |
} | |
} | |
int ans = 1e9; | |
for(int i=0;i<N;i++) ans = min(ans, d[N-1][i]); | |
return ans; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment