Skip to content

Commit 905e12e

Browse files
authored
Merge pull request #498 from AlgorithmWithGod/LiiNi-coder
[20250718] BOJ / G4 / 구간 나누기 2 / 이인희
2 parents 3875d98 + df684c7 commit 905e12e

File tree

1 file changed

+59
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)