Skip to content

Commit f6de73b

Browse files
authored
Create b2343.md
1 parent e965ea7 commit f6de73b

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

juseong/binary_search/b2343.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
```

0 commit comments

Comments
 (0)