120. Triangle

Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below.

For example, given the following triangle

[
     [2],
    [3,4],
   [6,5,7],
  [4,1,8,3]
]

The minimum path sum from top to bottom is 11 (i.e., 2 + 3 + 5 + 1 = 11).

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

阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u013250416/article/details/80329768
个人分类: leetcode
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭