-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathTriangle.cpp
More file actions
31 lines (29 loc) · 739 Bytes
/
Triangle.cpp
File metadata and controls
31 lines (29 loc) · 739 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
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
int minimumTotal(vector<vector<int>> &triangle)
{
int n = triangle.size();
vector<int> next_row(triangle[n - 1]);
vector<int> curr_row(n, 0);
for (int i = n - 2; i >= 0; i--)
{
for (int j = 0; j < i + 1; j++)
{
int lower_left = triangle[i][j] + next_row[j];
int lower_right = triangle[i][j] + next_row[j + 1];
curr_row[j] = min(lower_left, lower_right);
}
swap(curr_row, next_row);
}
return next_row[0]; // because we swapped at last iteration
}
};
int main()
{
int n;
cin >> n;
return 0;
}