File tree Expand file tree Collapse file tree 1 file changed +56
-0
lines changed Expand file tree Collapse file tree 1 file changed +56
-0
lines changed Original file line number Diff line number Diff line change
1
+
2
+ 어떤 한개를 기준으로 모두 되는지를 판단할때는 이분탐색 이용하면 좋을 것 같다
3
+
4
+ ```
5
+ package binary_search;
6
+
7
+ import java.util.*;
8
+
9
+ public class B2343 {
10
+ public static void main(String[] args) {
11
+ Scanner sc = new Scanner(System.in);
12
+ String[] str = sc.nextLine().split(" ");
13
+ int N = Integer.parseInt(str[0]); // 강의의 수
14
+ int M = Integer.parseInt(str[1]); // 블루레이 개수
15
+
16
+ String[] nums = sc.nextLine().split(" ");
17
+
18
+ int start = Arrays.stream(nums)
19
+ .mapToInt(Integer::parseInt)
20
+ .max().getAsInt();
21
+ int end = Arrays.stream(nums)
22
+ .mapToInt(Integer::parseInt)
23
+ .sum();
24
+
25
+ int result = Integer.MAX_VALUE;
26
+ while (start <= end) {
27
+ int mid = (start + end) / 2;
28
+
29
+ int count = 0;
30
+ int sum = 0;
31
+ for (String s : nums) {
32
+ int num = Integer.parseInt(s);
33
+ int ss = num + sum;
34
+ if (ss > mid) {
35
+ count++;
36
+ sum = num;
37
+ } else {
38
+ sum += num;
39
+ }
40
+ }
41
+ if (sum != 0) {
42
+ count++;
43
+ }
44
+
45
+ if (count <= M) {
46
+ result = Math.min(result, mid);
47
+ end = mid - 1;
48
+ } else {
49
+ start = mid + 1;
50
+ }
51
+ }
52
+ System.out.println(result);
53
+ }
54
+ }
55
+
56
+ ```
You can’t perform that action at this time.
0 commit comments