-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path13_MaximumPathSum.cpp
184 lines (164 loc) · 4.98 KB
/
13_MaximumPathSum.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// https://www.codingninjas.com/codestudio/problems/maximum-path-sum-in-the-matrix_797998
// You have been given an N*M matrix filled with integer numbers, find the maximum sum
// that can be obtained from a path starting from any cell in the first row to any cell
// in the last row.
// From a cell in a row, you can move to another cell directly below that row, or
// diagonally below left or right. So from a particular cell (row, col), we can move
// in three directions i.e.
// Down: (row+1,col)
// Down left diagonal: (row+1,col-1)
// Down right diagonal: (row+1, col+1)
#include <bits/stdc++.h>
using namespace std;
class Solution3
{
// Tabulation : Space optimisation
public:
int getMaxPathSum(vector<vector<int>> &matrix)
{
int m = matrix.size(), n = matrix[0].size();
vector<int> cp(n);
cp = matrix[0];
int subMax = INT_MIN;
for (int i = 1; i < m; ++i)
{
vector<int> temp(n);
for (int j = 0; j < n; ++j)
{
int up = matrix[i][j] + cp[j];
int right = INT_MIN;
if (j < n - 1)
right = matrix[i][j] + cp[j + 1];
int left = INT_MIN;
if (j > 0)
left = matrix[i][j] + cp[j - 1];
// dp[i][j] = max(up, right, left); Cannot compare 3 value: Giving error
int ans = max(left, right);
temp[j] = max(ans, up);
}
cp.swap(temp);
}
// Find max of last row
for (int j = 0; j < n; ++j)
{
subMax = max(subMax, cp[j]);
}
return subMax;
}
};
class Solution2
{
// Tabulation
public:
int getMaxPathSum(vector<vector<int>> &matrix)
{
int m = matrix.size(), n = matrix[0].size();
vector<vector<int>> dp(m, vector<int>(n));
dp[0] = matrix[0];
int subMax = INT_MIN;
for (int i = 1; i < m; ++i)
{
for (int j = 0; j < n; ++j)
{
int up = matrix[i][j] + dp[i - 1][j];
int right = INT_MIN;
if (j < n - 1)
right = matrix[i][j] + dp[i - 1][j + 1];
int left = INT_MIN;
if (j > 0)
left = matrix[i][j] + dp[i - 1][j - 1];
// dp[i][j] = max(up, right, left); Cannot compare 3 value: Giving error
int ans = max(left, right);
dp[i][j] = max(ans, up);
}
}
// Find max of last row
for (int j = 0; j < n; ++j)
{
subMax = max(subMax, dp[m - 1][j]);
}
return subMax;
}
};
class Solution1
{
// Recursive : Memoization
public:
int getMaxPathSum(vector<vector<int>> &matrix)
{
int m = matrix.size(), n = matrix[0].size();
vector<vector<int>> dp(m, vector<int>(n, -1));
int subMax = INT_MIN;
for (int i = 0; i < n; ++i)
{
dp[m - 1][i] = solve(matrix, dp, m - 1, i);
subMax = max(subMax, dp[m - 1][i]);
}
return subMax;
}
int solve(vector<vector<int>> &matrix, vector<vector<int>> &dp, int i, int j)
{
if (i == 0)
{
return matrix[i][j];
}
if (dp[i][j] != -1)
return dp[i][j];
// up : ALways valid
int up = matrix[i][j] + solve(matrix, dp, i - 1, j);
// right : Valid for (j < n-1)
int right = INT_MIN;
if (j < matrix[0].size() - 1)
right = matrix[i][j] + solve(matrix, dp, i - 1, j + 1);
// left : Valid for j > 0
int left = INT_MIN;
if (j > 0)
left = matrix[i][j] + solve(matrix, dp, i - 1, j - 1);
int ans = max(left, up);
return dp[i][j] = max(ans, right);
}
};
class Solution
{
// Recursive solution
public:
int getMaxPathSum(vector<vector<int>> &matrix)
{
int m = matrix.size(), n = matrix[0].size();
int subMax = INT_MIN;
for (int i = 0; i < n; ++i)
{
int x = solve(matrix, m - 1, i);
subMax = max(subMax, x);
}
return subMax;
}
int solve(vector<vector<int>> &matrix, int i, int j)
{
if (i == 0)
{
return matrix[i][j];
}
// up : ALways valid
int up = matrix[i][j] + solve(matrix, i - 1, j);
// right : Valid for (j < n-1)
int right = INT_MIN;
if (j < matrix[0].size() - 1)
right = matrix[i][j] + solve(matrix, i - 1, j + 1);
// left : Valid for j > 0
int left = INT_MIN;
if (j > 0)
left = matrix[i][j] + solve(matrix, i - 1, j - 1);
int ans = max(left, up);
return max(ans, right);
}
};
int main()
{
vector<vector<int>> matrix = {{1, 2, 10, 4}, {100, 3, 2, 1}, {1, 1, 20, 2}, {1, 2, 2, 1}};
Solution3 obj1;
cout << obj1.getMaxPathSum(matrix);
ios_base::sync_with_stdio(false);
cin.tie(NULL);
return 0;
}