int minimumTotal(vector<vector<int>>& triangle) {
const int INF = 10000;
vector<vector<int>> d(triangle);
for (int i = 1; i < d.size(); i++)
{
for (int j = 0; j < d[i].size(); j++)
{
int left = INF, right = INF;
if (j - 1 >= 0)
left = d[i - 1][j - 1];
if (j < d[i - 1].size())
right = d[i - 1][j];
d[i][j] = triangle[i][j] + min(left, right);
}
}
int min = d[d.size()-1][0];
for (int i = 1; i < d[d.size() - 1].size(); i++)
{
if (min > d[d.size() - 1][i])
min = d[d.size() - 1][i];
}
return min;
}
int main()
{
vector<vector<int>> triangle{ {2}, {3,4},{6,5,7},{4,1,8,3} };
cout<<minimumTotal(triangle);
system("pause");
return 0;
}
DP(4)
最新推荐文章于 2020-11-09 17:21:06 发布