-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMinimun_Falling_Path_Sum.cpp
84 lines (59 loc) · 2.01 KB
/
Minimun_Falling_Path_Sum.cpp
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// 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 ld=matrix[i][j]+f(i+1,j-1,n,matrix,dp);
int down=matrix[i][j]+f(i+1,j,n,matrix,dp);
int rd=matrix[i][j]+f(i+1,j+1,n,matrix,dp);
return dp[i][j]=min(ld,min(down,rd));
}
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;
}
};
// Time Complexity: O(N*N)
// Reason: At max, there will be M*N calls of recursion to solve a new problem,
// Space Complexity: O(N) + O(N*M)
// Reason: We are using a recursion stack space: O(N), where N is the path length and an external DP Array of size ‘N*M’.
// Tabulation
class Solution {
public:
int minFallingPathSum(vector<vector<int>>& matrix)
{
int n=matrix.size();
vector<vector<int>> dp(n, vector<int>(n,-1));
for(int j=0;j<n;j++) dp[n-1][j]=matrix[n-1][j];
for(int i=n-2;i>=0;i--)
{
for(int j=0;j<n;j++)
{
int ld =matrix[i][j];
if(i+1<n && j-1>=0)
ld += dp[i+1][j-1];
else ld+=1e9;
int down=matrix[i][j]+ dp[i+1][j];
int rd=matrix[i][j];
if(i+1<n && j+1<n)
rd+=dp[i+1][j+1];
else rd+=1e9;
dp[i][j]=min(ld,min(down, rd));
}
}
int mini=INT_MAX;
for(int i=0;i<n;i++)
{
mini=min(mini,dp[0][i]);
}
return mini;
}
};