Skip to content

Commit c004bb6

Browse files
committed
[DP] baekjoon-1932
1 parent e3f07c7 commit c004bb6

File tree

4 files changed

+123
-0
lines changed

4 files changed

+123
-0
lines changed

โ€ŽREADME.mdโ€Ž

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,11 @@
116116
| :-: | :----: | :------------------------------------------------------------------------------------------------------------------ | :------------------------ |
117117
| 01 | | [Baekjoon-1717 ์ง‘ํ•ฉ์˜ ํ‘œํ˜„](https://github.com/Seogeurim/Algorithm-practice/blob/master/src/Graph/P1717) | [Disjoint Set](https://github.com/Seogeurim/Algorithm-practice/blob/master/notes/Graph/DisjointSet.md) |
118118
| 02 | | [Baekjoon-2252 ์ค„ ์„ธ์šฐ๊ธฐ](https://github.com/Seogeurim/Algorithm-practice/blob/master/src/Graph/P2252) | [์œ„์ƒ์ •๋ ฌ](https://github.com/Seogeurim/Algorithm-practice/blob/master/notes/Graph/TopologicalSort.md) |
119+
120+
## Dynamic Programming
121+
122+
> ์•„์ง ๊ณต๋ถ€ํ•˜๊ธฐ ์ „์ด๋ผ ์ข‹์€ ์ฝ”๋“œ๊ฐ€ ์•„๋‹ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
123+
124+
| # | โ˜† | Problem | Note |
125+
| :-: | :----: | :------------------------------------------------------------------------------------------------------------------ | :------------------------ |
126+
| 01 | | [Baekjoon-1932 ์ •์ˆ˜ ์‚ผ๊ฐํ˜•](https://github.com/Seogeurim/Algorithm-practice/blob/master/src/DP/P1932) | |

โ€Žsrc/DP/P1932/Main.javaโ€Ž

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package DP.P1932;
2+
3+
import java.io.BufferedReader;
4+
import java.io.FileInputStream;
5+
import java.io.InputStreamReader;
6+
import java.util.StringTokenizer;
7+
8+
public class Main {
9+
10+
static int N;
11+
static int[] nums;
12+
13+
public static void main(String[] args) throws Exception {
14+
System.setIn(new FileInputStream("src/DP/P1932/input.txt"));
15+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
16+
17+
N = Integer.parseInt(br.readLine());
18+
nums = new int[N * (N + 1) / 2 + 1];
19+
20+
int count = 1;
21+
for (int i = 0; i < N; i++) {
22+
StringTokenizer st = new StringTokenizer(br.readLine());
23+
for (int j = 0; j <= i; j++) {
24+
int value = Integer.parseInt(st.nextToken());
25+
if (j == i) {
26+
nums[count] = nums[count - i - 1] + value;
27+
} else if (j == 0) {
28+
nums[count] = nums[count - i] + value;
29+
} else {
30+
int parent_max = Math.max(nums[count - i - 1], nums[count - i]);
31+
nums[count] = parent_max + value;
32+
}
33+
count ++;
34+
}
35+
}
36+
37+
int ans = 0;
38+
for (int i = 0; i < N; i++) {
39+
ans = Math.max(nums[nums.length - i - 1], ans);
40+
}
41+
System.out.println(ans);
42+
}
43+
}

โ€Žsrc/DP/P1932/README.mdโ€Ž

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
### [baekjoon-1932] ์ •์ˆ˜ ์‚ผ๊ฐํ˜•
2+
3+
![image](https://user-images.githubusercontent.com/22045163/90809734-e2376900-e35c-11ea-9ea4-5d24978f43aa.png)
4+
5+
> ๋””๋ฒ„๊น…์šฉ ์ฝ”๋“œ
6+
7+
```java
8+
import java.io.BufferedReader;
9+
import java.io.FileInputStream;
10+
import java.io.InputStreamReader;
11+
import java.util.Arrays;
12+
import java.util.StringTokenizer;
13+
14+
public class Main {
15+
16+
static int N;
17+
static Node[] nums;
18+
19+
public static void main(String[] args) throws Exception {
20+
System.setIn(new FileInputStream("src/DP/P1932/input.txt"));
21+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
22+
23+
N = Integer.parseInt(br.readLine());
24+
25+
nums = new Node[N * (N + 1) / 2 + 1];
26+
nums[0] = new Node(0, 0);
27+
28+
int count = 1;
29+
for (int i = 0; i < N; i++) {
30+
StringTokenizer st = new StringTokenizer(br.readLine());
31+
for (int j = 0; j <= i; j++) {
32+
int value = Integer.parseInt(st.nextToken());
33+
if (j == i) {
34+
nums[count] = new Node(value, nums[count - i - 1].sum + value);
35+
} else if (j == 0) {
36+
nums[count] = new Node(value, nums[count - i].sum + value);
37+
} else {
38+
int parent_max = Math.max(nums[count - i - 1].sum, nums[count - i].sum);
39+
nums[count] = new Node(value, parent_max + value);
40+
}
41+
count ++;
42+
}
43+
}
44+
System.out.println(Arrays.toString(nums));
45+
}
46+
}
47+
48+
class Node {
49+
int value;
50+
int sum;
51+
52+
public Node(int value, int sum) {
53+
this.value = value;
54+
this.sum = sum;
55+
}
56+
57+
@Override
58+
public String toString() {
59+
return "Node{" +
60+
"value=" + value +
61+
", sum=" + sum +
62+
'}';
63+
}
64+
}
65+
66+
```

โ€Žsrc/DP/P1932/input.txtโ€Ž

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
5
2+
7
3+
3 8
4+
8 1 0
5+
2 7 4 4
6+
4 5 2 6 5

0 commit comments

Comments
ย (0)