其他分享
首页 > 其他分享> > 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());
    }
};

  

标签:triangle,min,int,element,vector,3.30,Triangle,动态,刷题
来源: https://www.cnblogs.com/zhangdalao/p/14598782.html