-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtriangle.cpp
More file actions
40 lines (34 loc) · 1.17 KB
/
Copy pathtriangle.cpp
File metadata and controls
40 lines (34 loc) · 1.17 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
class Solution {
public:
/**
* @param triangle: a list of lists of integers.
* @return: An integer, minimum path sum.
*/
int minimumTotal(vector<vector<int> > &triangle) {
// write your code here
// f[i][j] is first i step minimus cost arrive at j
// f[i][j] = min (f[i - 1][j], f[i-1][j-1] + A[i][j]
int levels = triangle.size();
vector<vector<int>> f(levels, vector<int>(levels, 0));
f[0][0] = triangle[0][0];
for (int i = 1; i < levels; i++) {
for (int j = 0; j <= i; j++) {
if (j == i) {
f[i][j] = f[i - 1][j - 1];
} else if (j == 0) {
f[i][j] = f[i - 1][j];
} else {
f[i][j] = f[i - 1][j - 1] < f[i - 1][j] ? f[i - 1][j - 1] : f[i - 1][j];
}
f[i][j] += triangle[i][j];
}
}
int cost = f[levels - 1][0];
for (int i = 1; i < levels; i++) {
if (f[levels - 1][i] < cost) {
cost = f[levels - 1][i];
}
}
return cost;
}
};