- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlargesttriangle_1.cpp
More file actions
Latest commit
44 lines (37 loc) · 787 Bytes
/
Copy pathlargesttriangle_1.cpp
File metadata and controls
44 lines (37 loc) · 787 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
33
34
35
36
37
38
39
40
41
42
43
44
#include<iostream>
usingnamespacestd;
voidprint(int dp[][4], int s, int e, int n, int m, int in[][4])
{
if (s>=4)
return;
cout<<in[s][e]<< "";
if (dp[n+1][m] > dp[n][m - 1])
print(dp, s + 1, e + 1, n + 1, m, in);
else
print(dp, s + 1, e, n, m-1, in);
}
intmaxSum(int in[][4], int n)
{
int dp[4][4];
memset(dp, 0, sizeof(dp));
for(int i = 0; i< n; ++i)
{
dp[i][i] = in[n-1][i];
}
for (int gap = 1; gap < n; ++gap)
{
for (int i = 0, j = gap; j < n; ++i, ++j)
{
dp[i][j] = in[n - 1 - gap][i] + max(dp[i + 1][j], dp[i][j - 1]);
}
}
print(dp, 0,0, 0, n-1, in);
return dp[0][n-1];
}
intmain()
{
int in[4][4] = {{3,0,0,0},{10,7,0,0},{12,14,13,0},{20,19,23,16}};
printf ("\n max sum in triangle %d", maxSum(in, 4));
getchar();
return0;
}