-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinimumTotal.cpp
More file actions
26 lines (22 loc) · 788 Bytes
/
MinimumTotal.cpp
File metadata and controls
26 lines (22 loc) · 788 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
class Solution {
public:
int minimumTotal(vector<vector<int>>& triangle) {
int n = triangle.size();
vector dp(n, 10010);
dp[0] = triangle[0][0];
for (int i = 1; i < n; ++i) {
for (int j = i; j > 0; --j) {
dp[j] = std::min(dp[j - 1], dp[j]) + triangle[i][j];
}
dp[0] += triangle[i][0];
}
return *std::min_element(dp.begin(), dp.end());
}
int minimumTotal2(vector<vector<int>>& triangle) {
int n = triangle.size();
for(int i = n - 2; i >= 0; i--)
for(int j = 0; j <= i; j++)
triangle[i][j] += std::min(triangle[i + 1][j], t[i + 1][j + 1]); // chossing the optimal one
return triangle[0][0];
}
};