// O(n)的额外空间
class Solution {
public:
int minimumTotal(vector<vector<int>>& triangle) {
vector<int> res(triangle.size(), 0);
res[0] = triangle[0][0];
// 从上往下,从右往左遍历
for (int i = 1; i < triangle.size(); ++i){
for (int j = i; j >= 0; --j){
if (j == 0)
res[j] += triangle[i][j];
else if (j == i)
res[j] = res[j - 1] + triangle[i][j];
else
res[j] = min(res[j - 1], res[j]) + triangle[i][j];
}
}
return *min_element(res.begin(),res.end());
}
};
static int x=[](){
std::ios::sync_with_stdio(false);
cin.tie(NULL);
return 0;
}();