动态规划

120. Triangle

2016-12-07  本文已影响0人  yangqi916

从底部往上算,递归公式:

#include<vector>
#include<algorithm>
#include<cstdio>
#include <unordered_map>
#include <cmath>
#include <string>
using namespace std;

class Solution {
private:
    int min(int a, int b)
    {
        return a < b? a : b;
    }
    
public:
    int minimumTotal(vector<vector<int>>& triangle) {
        int row = (int)triangle.size();
        
        for (int i = row - 2; i>=0; i--)
        {
            for (int j = 0; j <= i; j++)
            {
                triangle[i][j] += min(triangle[i+1][j+1], triangle[i+1][j]);
            }
        }
        
        return triangle[0][0];
    }
};
上一篇下一篇

猜你喜欢

热点阅读