-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlc120.cpp
More file actions
30 lines (22 loc) · 798 Bytes
/
lc120.cpp
File metadata and controls
30 lines (22 loc) · 798 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
27
28
29
30
#include <vector>
using namespace std;
class Solution {
public:
int minimumTotal(vector<vector<int>>& triangle) {
int nrows = triangle.size();
vector<vector<int>> dp(nrows, vector<int>(nrows));
dp[0][0] = triangle[0][0];
for(int i = 1; i < nrows; i++) {
for(int j = 0; j < triangle[i].size(); j++) {
if (j == 0)
dp[i][j] = dp[i-1][j] + triangle[i][j];
else if (j == triangle[i].size() - 1)
dp[i][j] = dp[i-1][j-1] + triangle[i][j];
else
dp[i][j] = min(dp[i-1][j], dp[i-1][j-1]) + triangle[i][j];
}
}
auto min_it = std::min_element(dp[nrows-1].begin(), dp[nrows-1].end());
return *min_it;
}
};