-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtriangle.cpp
40 lines (32 loc) · 1.05 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
38
39
40
// Completely mis-calculated the result
// class Solution {
// public:
// int minimumTotal(vector<vector<int>>& triangle) {
// int res = 0;
// int prevIndex = 0;
// for (auto stage: triangle) {
// int n = size(stage);
// int minInStage = INT_MAX;
// int limit = min(n, prevIndex + 2);
// for(int i = prevIndex; i < limit; i++) {
// if (stage[i] < minInStage) {
// minInStage = stage[i];
// prevIndex = i;
// }
// }
// cout<<minInStage<<endl;
// res += minInStage;
// }
// return res;
// }
// };
class Solution {
public:
int minimumTotal(vector<vector<int>>& triangle) {
int n = triangle.size();
for(int i= n-2; i >= 0; i--)
for(int j = 0; j <= i; j++)
triangle[i][j] += min(triangle[i+1][j], triangle[i+1][j+1]);
return triangle[0][0];
}
};