美文网首页Leetcode
Leetcode 120. Triangle

Leetcode 120. Triangle

作者: SnailTyan | 来源:发表于2018-12-13 21:49 被阅读1次

文章作者:Tyan
博客:noahsnail.com  |  CSDN  |  简书

1. Description

Triangle

2. Solution

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

Reference

  1. https://leetcode.com/problems/triangle/description/

相关文章

网友评论

    本文标题:Leetcode 120. Triangle

    本文链接:https://www.haomeiwen.com/subject/ktdihqtx.html