solution.cpp 594 字节
Newer Older
每日一练社区's avatar
每日一练社区 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
#include <vector>
#include <algorithm>
#include <iostream>

using namespace std;

class Solution
{
public:
    int minimumTotal(vector<vector<int>> &triangle)
    {
        vector<int> steps;
        for (auto &v : triangle)
        {
            if (!steps.empty())
            {
                v.front() += steps.front();
                v.back() += steps.back();
            }
            for (size_t i = 1; i < steps.size(); ++i)
                v[i] += min(steps.at(i - 1), steps.at(i));
            steps = v;
        }
        return *min_element(steps.cbegin(), steps.cend());
    }
};