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

#include <iostream>
#include <vector>

using namespace std;

int minimumTotal(vector<vector<int> > &triangle) {
    size_t size = triangle.size();

    if (size <= 0) {
        return 0;
    }
    
    int row = size - 1;
    while (--row >= 0) {
        int colSize = triangle[row].size();
        int i = row + 1;
        for (int j = 0; j < colSize; ++j) {
            int min = triangle[i][j] < triangle[i][j+1] ?
                        triangle[i][j] : triangle[i][j+1];
            triangle[row][j] += min;
        }
    }

    return triangle[0][0];
}
View Program Text


Test Status