方法一:动态规划
class Solution {
public:
int minimumTotal(vector<vector<int>>& triangle) {
int n = triangle.size();
vector<vector<int>> dp(n, vector<int>(n));
dp[0][0] = triangle[0][0];
for(int i = 1; i < n; i++){
dp[i][0] = dp[i - 1][0] + triangle[i][0];
for(int j = 1; j < triangle[i].size() - 1; j++){
dp[i][j] = min(dp[i - 1][j], dp[i - 1][j - 1]) + triangle[i][j];
}
dp[i][i] = dp[i - 1][i - 1] + triangle[i][i];
}
return *min_element(dp[n - 1].begin(), dp[n - 1].end());
}
};
方法二:动态规划 + 空间优化
class Solution {
public:
int minimumTotal(vector<vector<int>>& triangle) {
int n = triangle.size();
vector<vector<int>> dp(n, vector<int>(n));
dp[0][0] = triangle[0][0];
for(int i = 1; i < n; i++){
dp[i][0] = dp[i - 1][0] + triangle[i][0];
for(int j = 1; j < triangle[i].size() - 1; j++){
dp[i][j] = min(dp[i - 1][j], dp[i - 1][j - 1]) + triangle[i][j];
}
dp[i][i] = dp[i - 1][i - 1] + triangle[i][i];
}
return *min_element(dp[n - 1].begin(), dp[n - 1].end());
}
};
Accepted
44/44 cases passed (4 ms)
Your runtime beats 93.28 % of cpp submissions
Your memory usage beats 71.74 % of cpp submissions (8.4 MB)