Skip to content

Commit 7c955fc

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

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

โ€Žsrc/backjoon/_1932.java

+58
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)