-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTrianglePath.java
More file actions
28 lines (26 loc) · 854 Bytes
/
TrianglePath.java
File metadata and controls
28 lines (26 loc) · 854 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
class TrianglePath {
public int solution(int[][] triangle) {
int n = triangle.length;
// DP 배열 갱신
for (int i = 1; i < n; i++) {
for (int j = 0; j <= i; j++) {
if (j == 0) {
// 왼쪽 끝
triangle[i][j] += triangle[i - 1][j];
} else if (j == i) {
// 오른쪽 끝
triangle[i][j] += triangle[i - 1][j - 1];
} else {
// 가운데
triangle[i][j] += Math.max(triangle[i - 1][j - 1], triangle[i - 1][j]);
}
}
}
// 마지막 행의 최댓값 반환
int max = 0;
for (int num : triangle[n - 1]) {
max = Math.max(max, num);
}
return max;
}
}