标签:
2
3,4
6,5,7
4,1,8,3
2 + 3 + 5 + 1 = 11
思路:从顶点开始将每一层的点更新为到达该点所需要的最小开销,最终查找底层的最小值即可。
class Solution {
public:
int minimumTotal(vector<vector<int> > &triangle) {
if (triangle.empty())
{
return 0;
}
for (size_t i = 1; i < triangle.size(); i++)
{
vector<int>& preTri = triangle[i - 1];
vector<int>& currentTri = triangle[i];
currentTri[0] += preTri[0];
for (size_t j = 1; j < currentTri.size()-1; j++)
{
currentTri[j] += min(preTri[j - 1], preTri[j]);
}
currentTri[currentTri.size() - 1] += preTri[preTri.size() - 1];
}
int min = INT_MAX;
int triNum = triangle.size()-1;
for (size_t i = 0; i < triangle[triNum].size(); i++)
{
if (triangle[triNum][i]<min)
{
min = triangle[triNum][i];
}
}
return min;
}
};
标签:
原文地址:http://www.cnblogs.com/flyjameschen/p/e3e2cd60bfe4995b81facd265ee0adb7.html