-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTriangle.cpp
36 lines (32 loc) · 1.06 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
// 120. Triangle
// Medium. faster than 73.01%.
// Solution implemented using same space as the given 'triangle' vector
class Solution {
public:
int minimumTotal(vector<vector<int>>& triangle) {
int m = triangle.size();
vector< vector<int> > res;
int min_path_sum = INT_MAX;
res = triangle;
for(int i = 0; i < res.size(); ++i)
{
for(int j = 0; j < res[i].size(); ++j)
{
if(i == 0)
res[i][j] = triangle[i][j];
else
{
int l = INT_MAX, r = INT_MAX;
if(j - 1 >= 0)
l = res[i-1][j-1];
if(j < i)
r = res[i-1][j];
res[i][j] = triangle[i][j] + min(l, r);
}
}
}
for(int j = 0; j < triangle[m-1].size(); ++j)
min_path_sum = min(min_path_sum, res[m-1][j]);
return min_path_sum;
}
};