-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path120_Triangle.cpp
More file actions
51 lines (43 loc) · 1.3 KB
/
120_Triangle.cpp
File metadata and controls
51 lines (43 loc) · 1.3 KB
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
46
47
48
49
50
51
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class Solution {
public:
int minimumTotal(vector<vector<int>>& triangle) {
int n = triangle.size();
if (n == 0) return 0;
else if (n == 1) return triangle[0][0];
vector<int> f(triangle[n - 1]);
for (int i = n - 2; i >= 0; i--){
for (int j = 0; j <= i; j++){
f[j] = triangle[i][j] + min(f[j], f[j + 1]);
}
}
return f[0];
}
int minimumTotal2(vector<vector<int>>& triangle) {
if (triangle.size() == 0) return 0;
else if (triangle.size() == 1) return triangle[0][0];
for (int i = triangle.size()-2; i >= 0; i--){
for (int j = 0; j < triangle[i].size(); j++){
triangle[i][j] += min(triangle[i+1][j],triangle[i+1][j+1]);
}
}
return triangle[0][0];
}
};
int main() {
vector<vector<int>> triangle;
int a1[] = {2};
int a2[] = {3,4};
int a3[] = {6,5,7};
int a4[] = {4,1,8,3};
triangle.push_back(vector<int>(a1,a1+1));
triangle.push_back(vector<int>(a2,a2+2));
triangle.push_back(vector<int>(a3,a3+3));
triangle.push_back(vector<int>(a4,a4+4));
Solution s;
cout << s.minimumTotal(triangle) << endl;
return 0;
}