-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path120.cpp
More file actions
19 lines (19 loc) · 723 Bytes
/
Copy path120.cpp
File metadata and controls
19 lines (19 loc) · 723 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// dp.cpp
class Solution {
public:
int minimumTotal(vector<vector<int>>& triangle) {
int row = triangle.size();
vector<vector<int>> dp{triangle[0]};
for (int i = 0; i < row - 1; ++i) {
dp.push_back(vector<int>(triangle[i + 1].size(), 0x7fffffff));
for (int j = 0; j < triangle[i].size(); ++j) {
for (int x = 0; x <= 1; ++x)
if (j + x >= 0 && j + x < triangle[i + 1].size())
dp[i + 1][j + x] =
min(dp[i + 1][j + x],
triangle[i + 1][j + x] + dp[i][j]);
}
}
return *min_element(dp.back().begin(), dp.back().end());
}
};