題目:
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below.
For example, given the following triangle
[ [2], [3,4], [6,5,7], [4,1,8,3] ]
The minimum path sum from top to bottom is 11
(i.e., 2 + 3 + 5 + 1 =
11).
從下到上,第一層得到的就是到第2層的最短路的最小值加第一層的數。
代碼如下:
class Solution {
public:
int minimumTotal(vector<vector<int>>& triangle) {
int rows = triangle.size();
vector<int>minm = triangle[triangle.size()-1];
for(int row = rows - 2; row >= 0; --row){
for(int i = 0; i <= row; ++i){
minm[i] = min(minm[i], minm[i+1])+triangle[row][i];
}
}
return minm[0];
}
};