Skip to content

Commit 43523ac

Browse files
authored
Merge pull request #1355 from AlgorithmWithGod/Seol-JY
[20251108] BOJ / G5 / 기타 레슨 / 설진영
2 parents 6ad7698 + c0cbb49 commit 43523ac

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
```java
2+
import java.io.*;
3+
import java.util.*;
4+
5+
public class Main {
6+
public static void main(String[] args) throws IOException {
7+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
8+
StringTokenizer st = new StringTokenizer(br.readLine());
9+
10+
int N = Integer.parseInt(st.nextToken());
11+
int M = Integer.parseInt(st.nextToken());
12+
13+
int[] lectures = new int[N];
14+
st = new StringTokenizer(br.readLine());
15+
16+
int left = 0;
17+
int right = 0;
18+
19+
for (int i = 0; i < N; i++) {
20+
lectures[i] = Integer.parseInt(st.nextToken());
21+
left = Math.max(left, lectures[i]);
22+
right += lectures[i];
23+
}
24+
25+
int answer = right;
26+
27+
while (left <= right) {
28+
int mid = (left + right) / 2;
29+
30+
if (canDivide(lectures, M, mid)) {
31+
answer = mid;
32+
right = mid - 1;
33+
} else {
34+
left = mid + 1;
35+
}
36+
}
37+
38+
System.out.println(answer);
39+
}
40+
41+
private static boolean canDivide(int[] lectures, int M, int size) {
42+
int count = 1;
43+
int currentSum = 0;
44+
45+
for (int lecture : lectures) {
46+
if (currentSum + lecture > size) {
47+
count++;
48+
currentSum = lecture;
49+
50+
if (count > M) {
51+
return false;
52+
}
53+
} else {
54+
currentSum += lecture;
55+
}
56+
}
57+
58+
return true;
59+
}
60+
}
61+
```

0 commit comments

Comments
 (0)