简介
自底部向上
使用回溯
超时算法
class Solution {
public:
int minValue;
void dfs(vector<vector<int>>& triangle, int value, int depth, int index){
if(depth == triangle.size()){
if(minValue > value){
minValue = value;
}
return;
}
dfs(triangle, value + triangle[depth][index], depth + 1, index);
dfs(triangle, value + triangle[depth][index], depth + 1, index + 1);
}
int minimumTotal(vector<vector<int>>& triangle) {
if(triangle.size() == 0){
return 0;
}
minValue = 100000;
dfs(triangle, 0, 0, 0);
return minValue;
}
};
自底向上
class Solution {
public:
int minimumTotal(vector<vector<int>>& fuckDP) {
for (int i = fuckDP.size() - 2; i >= 0; --i)
for (int j = 0; j < fuckDP[i].size(); ++j)
fuckDP[i][j] += min(fuckDP[i + 1][j], fuckDP[i + 1][j + 1]);
return fuckDP[0][0];
}
};