-
Notifications
You must be signed in to change notification settings - Fork 257
/
triangle.cpp
38 lines (32 loc) · 1.15 KB
/
triangle.cpp
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
27
28
29
30
31
32
33
34
35
36
37
// Time: O(n)
// Space: O(n)
#include <algorithm>
#include <vector>
using std::min;
using std::vector;
class Solution {
public:
/**
* @param triangle: a list of lists of integers.
* @return: An integer, minimum path sum.
*/
int minimumTotal(vector<vector<int>> &triangle) {
// for empty input
if (triangle.empty()) {
return 0;
}
// Stores the minimum path sum of triangle[i - 1].
vector<int> prev_row(triangle.front());
for (size_t i = 1; i < triangle.size(); ++i) {
// Stores the minimum path sum of triangle[i].
vector<int> curr_row(triangle[i]);
curr_row.front() += prev_row.front(); // For the first element.
for (size_t j = 1; j < curr_row.size() - 1; ++j) {
curr_row[j] += min(prev_row[j - 1], prev_row[j]);
}
curr_row.back() += prev_row.back(); // For the last element.
prev_row.swap(curr_row);
}
return *min_element(prev_row.cbegin(), prev_row.cend());
}
};