-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path120Triangle.cpp
More file actions
39 lines (36 loc) · 926 Bytes
/
120Triangle.cpp
File metadata and controls
39 lines (36 loc) · 926 Bytes
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
#include <bits/stdc++.h>
using namespace std;
void print(vector<vector<int>>&t)
{
for(vector<int>v:t)
{
for(int x:v)
{
cout << x << " ";
}
cout << endl;
}
}
int minimumTotal(vector<vector<int>>& triangle) {
int idx=0;
int n = triangle.size();
//bottom up approach
for(int i=n-1;i>0;i--)
{
//each level
vector<int>consideration = triangle[i];
vector<int>upar = triangle[i-1];
int size = upar.size();
for(int j=0;j<size;j++)
{
upar[j] += min(consideration[j], consideration[j+1]);
}
if(size==1)
{
return upar[0];
}
triangle[i-1] = upar;
// print(triangle);
}
return triangle[0][0];
}