-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path200207-1.cpp
45 lines (43 loc) · 898 Bytes
/
200207-1.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
38
39
40
41
42
43
44
45
// https://leetcode-cn.com/problems/triangle/
#include <cstdio>
#include <vector>
using namespace std;
class Solution {
public:
int minimumTotal(vector<vector<int>>& triangle) {
if (triangle.empty()) return 0;
vector<int> a{triangle[0][0]};
for (int i = 1; i < triangle.size(); ++i) {
vector<int> b;
for (int j = 0; j < triangle[i].size(); ++j) {
if (j == 0) {
b.push_back(a[j] + triangle[i][j]);
} else if (j + 1 < triangle[i].size()) {
b.push_back(min(a[j-1], a[j]) + triangle[i][j]);
} else {
b.push_back(a[j-1] + triangle[i][j]);
}
}
a = b;
}
int n = a[0];
for (int i = 1; i < a.size(); ++i) {
if (n > a[i]) n = a[i];
}
return n;
}
};
int main()
{
Solution s;
{
vector<vector<int>> a = {
{2},
{3,4},
{6,5,7},
{4,1,8,3}
};
printf("%d\n", s.minimumTotal(a)); // answer: 11 (2 + 3 + 5 + 1 = 11)
}
return 0;
}