forked from sajdakabir/dp-dynamic-programming-series
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinimum_Falling_Path_Sum_II.cpp
More file actions
32 lines (27 loc) · 858 Bytes
/
Copy pathMinimum_Falling_Path_Sum_II.cpp
File metadata and controls
32 lines (27 loc) · 858 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// memoization
class Solution {
public:
int f(int i,int j,int n,vector<vector<int>>& matrix ,vector<vector<int>>& dp){
if(j<0 || j>=n)return 1e9;
if(i==n-1) return matrix[n-1][j];
if(dp[i][j]!=-1)return dp[i][j];
int mini=1e9;
for(int newCol=0; newCol<n;newCol++){
if(newCol!=j){
int ans=matrix[i][j]+f(i+1,newCol,n,matrix,dp);
mini=min(mini,ans);
}
}
return dp[i][j]=mini;
}
int minFallingPathSum(vector<vector<int>>& matrix) {
int n=matrix.size();
vector<vector<int>> dp(n,vector<int>(n,-1));
int mini=INT_MAX;
for(int j=0;j<n;j++){
int ans=f(0,j,n,matrix,dp);
mini=min(ans,mini);
}
return mini;
}
};