Skip to content

Commit 7c955fc

Browse files
committed
issue #43 1932
1 parent ea70ce3 commit 7c955fc

1 file changed

Lines changed: 58 additions & 0 deletions

File tree

src/backjoon/_1932.java

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package backjoon;
2+
// https://www.acmicpc.net/problem/1932
3+
4+
import java.io.BufferedReader;
5+
import java.io.InputStreamReader;
6+
import java.io.IOException;
7+
import java.util.StringTokenizer;
8+
9+
public class _1932 {
10+
static int[][] arr; //삼각형이 저장되는 2차원배열
11+
static Integer[][] dp;
12+
static int N;
13+
14+
// memory 26600 runtime 260
15+
public static void main(String[] args) throws IOException {
16+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
17+
N = Integer.parseInt(br.readLine());
18+
arr = new int[N][N];
19+
dp = new Integer[N][N];
20+
StringTokenizer st;
21+
for (int i = 0; i < N; i++) {
22+
st = new StringTokenizer(br.readLine(), " ");
23+
24+
for (int j = 0; j < i + 1; j++) {
25+
arr[i][j] = Integer.parseInt(st.nextToken());
26+
}
27+
}
28+
for (int i = 0; i < N; i++) {
29+
dp[N - 1][i] = arr[N - 1][i];
30+
}
31+
32+
System.out.println(func(0, 0));
33+
}
34+
static int func(int depth, int idx) {
35+
// 마지막 행일 경우 현재 위치의 dp값 반환
36+
if(depth == N - 1)
37+
return dp[depth][idx];
38+
39+
// 탐색하지 않았던 값일 경우 다음 행의 양쪽 값 비교
40+
if (dp[depth][idx] == null) {
41+
dp[depth][idx] = Math.max(func(depth + 1, idx), func(depth + 1, idx + 1)) + arr[depth][idx];
42+
}
43+
return dp[depth][idx];
44+
45+
}
46+
}
47+
/*
48+
input
49+
5
50+
7
51+
3 8
52+
8 1 0
53+
2 7 4 4
54+
4 5 2 6 5
55+
56+
output
57+
30
58+
*/

0 commit comments

Comments
 (0)