数字三角形
递归
int solve(int i,int j)
{
return a[i][j]+(i==n?0:max(solve(i+1,j),slove(i+1,j+1)));
}
- 时间效率低
- 重复计算
pass
递推
int i,j;
for (j=1;j<=n;j++)
d[n][j]=a[n][j];
for (i=n-1;i>=1;i--)
for (j=1;j<=i;j++)
d[i][j]=a[i][j]+max(d[i+1][j],d[i+1][j+1]);
记忆化搜索
int solve(int i,int j)
{
if (d[i][j]>=0)
return d[i][j];
return d[i][j]=a[i][j]+(i==n?0:max(solve(i+1,j),slove(i+1,j+1)));
}