-
Notifications
You must be signed in to change notification settings - Fork 38
/
solve.cpp
68 lines (67 loc) · 1.62 KB
/
solve.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
class Solution {
public:
int minimumTotal(vector<vector<int>> &a) {
return compress_minimumTotal(a);
}
int positive_minimumTotal(vector<vector<int>> &a) {
int n = a.size();
if (n < 1)
return 0;
vector<vector<int>> dp(n, vector<int>(n, 0));
dp[0][0] = a[0][0];
for (int i = 1; i < n; ++i) {
dp[i][0] = dp[i - 1][0] + a[i][0];
dp[i][i] = dp[i - 1][i - 1] + a[i][i];
for (int j = 1; j < i; ++j) {
dp[i][j] = min(dp[i - 1][j], dp[i - 1][j - 1]) + a[i][j];
}
}
return *min_element(begin(dp[n - 1]), end(dp[n - 1]));
}
int inversed_minimumTotal(const vector<vector<int>> &a) {
int n = a.size();
if (n < 1)
return 0;
vector<vector<int>> dp(n, vector<int>(n, 0));
for (int i = 0; i < a[n - 1].size(); ++i) {
dp[n - 1][i] = a[n - 1][i];
}
for (int i = n - 2; i >= 0; --i) {
for (int j = 0; j <= i; ++j) {
dp[i][j] = min(dp[i + 1][j], dp[i + 1][j + 1]) + a[i][j];
}
}
return dp[0][0];
}
int compress_minimumTotal(const vector<vector<int>> &a) {
int n = a.size();
if (n < 1)
return 0;
vector<int> dp;
for_each(begin(a[n - 1]), end(a[n - 1]), [&dp](int i){dp.push_back(i);});
for (int i = n - 2; i >= 0; --i) {
for (int j = 0; j <= i; ++j) {
dp[j] = min(dp[j], dp[j + 1]) + a[i][j];
}
}
return dp[0];
}
};
int main(int argc, char **argv)
{
Solution solution;
vector<vector<int>> triangle = {
{2},
{3,4},
{6,5,7},
{4,1,8,3}
};
cout << solution.minimumTotal(triangle) << endl;
}