-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQ33.cpp
More file actions
29 lines (24 loc) · 796 Bytes
/
Copy pathQ33.cpp
File metadata and controls
29 lines (24 loc) · 796 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
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int dp[501][501];
int solution(vector<vector<int>> triangle) {
int answer = 0;
int idx = 0;
for (int i = 0; i < triangle.size(); i++) {
for (int j = 0; j < triangle[i].size(); j++) {
dp[i][j] = triangle[i][j];
}
}
for (int i = 0; i < triangle.size() - 1; i++) {
for (int j = 0; j < triangle[i].size(); j++) {
dp[i + 1][j] = max(dp[i + 1][j], dp[i][j] + triangle[i + 1][j]);
dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i][j] + triangle[i + 1][j + 1]);
}
}
for (int i = 0; i < triangle[triangle.size() - 1].size(); i++) {
answer = max(answer, dp[triangle.size() - 1][i]);
}
return answer;
}