3.30刷题记录 Triangle

3.30刷题记录 Triangle

 

 动态规划题,根据动态规划,选择最短的路径

class Solution {
public:
    int minimumTotal(vector<vector<int>>& triangle) {
        int n = triangle.size();
        vector<int> f(n);
        f[0] = triangle[0][0];
        for (int i = 1; i < n; ++i) {
            f[i] = f[i - 1] + triangle[i][i];
            for (int j = i - 1; j > 0; --j) {
                f[j] = min(f[j - 1], f[j]) + triangle[i][j];
            }
            f[0] += triangle[i][0];
        }
        return *min_element(f.begin(), f.end());
    }
};

  

上一篇:A. Ichihime and Triangle 叮叮叮


下一篇:三角形最小路径和