-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaxTrianglePathSum.cpp
54 lines (38 loc) · 1.07 KB
/
maxTrianglePathSum.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
#include<bits/stdc++.h>
using namespace std;
#define R 4
#define C 4
//Maximum path sum in a triangle.
int findMaxSumPath(int triangle[][C],int i,int j){
if(i>=R || j>=C)
return 0;
return triangle[i][j] + max(findMaxSumPath(triangle,i+1,j),findMaxSumPath(triangle,i+1,j+1));
}
// dynamic solution for the above recursion solution
int maxPathSum(int tri[][C], int m, int n)
{
// loop for bottom-up calculation
for (int i=m-1; i>=0; i--)
{
for (int j=0; j<=i; j++)
{
tri[i][j] += max(tri[i+1][j],tri[i+1][j+1]);
}
}
return tri[0][0];
}
int main(){
int triangle[][C] = {
{3,0,0,0},{7,4,0,0},
{2,4,6,0},{8,5,9,3}
};
int mxSum = maxPathSum(triangle,R-1,C-1);
cout<<mxSum<<endl;
int triangle2[][C] = {
{8,0,0,0},{-4,4,0,0},
{2,2,6,0},{1,1,1,1}
};
mxSum = maxPathSum(triangle2,R-1,C-1);
cout<<mxSum<<endl;
return 0;
}