diff --git "a/Seol-JY/202511/15 BOJ G4 \352\265\254\352\260\204 \353\202\230\353\210\204\352\270\260 2.md" "b/Seol-JY/202511/15 BOJ G4 \352\265\254\352\260\204 \353\202\230\353\210\204\352\270\260 2.md" new file mode 100644 index 00000000..fa804f69 --- /dev/null +++ "b/Seol-JY/202511/15 BOJ G4 \352\265\254\352\260\204 \353\202\230\353\210\204\352\270\260 2.md" @@ -0,0 +1,68 @@ +```java +import java.io.*; +import java.util.*; + +public class Main { + static int N, M; + static int[] arr; + + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringTokenizer st = new StringTokenizer(br.readLine()); + + N = Integer.parseInt(st.nextToken()); + M = Integer.parseInt(st.nextToken()); + + arr = new int[N]; + st = new StringTokenizer(br.readLine()); + + int maxVal = 0; + int minVal = Integer.MAX_VALUE; + + for (int i = 0; i < N; i++) { + arr[i] = Integer.parseInt(st.nextToken()); + maxVal = Math.max(maxVal, arr[i]); + minVal = Math.min(minVal, arr[i]); + } + + int left = 0; + int right = maxVal - minVal; + int answer = right; + + while (left <= right) { + int mid = (left + right) / 2; + + if (canDivide(mid)) { + answer = mid; + right = mid - 1; + } else { + left = mid + 1; + } + } + + System.out.println(answer); + } + + static boolean canDivide(int maxScore) { + int sections = 1; + int minInSection = arr[0]; + int maxInSection = arr[0]; + + for (int i = 1; i < N; i++) { + int newMin = Math.min(minInSection, arr[i]); + int newMax = Math.max(maxInSection, arr[i]); + + if (newMax - newMin > maxScore) { + sections++; + minInSection = arr[i]; + maxInSection = arr[i]; + } else { + minInSection = newMin; + maxInSection = newMax; + } + } + + return sections <= M; + } +} +```