-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMusicVideo.java
More file actions
46 lines (42 loc) · 1.16 KB
/
MusicVideo.java
File metadata and controls
46 lines (42 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package algorithm.sortingAndSearching;
import java.util.Arrays;
import java.util.Scanner;
public class MusicVideo {
public static int solution(int n, int m, int[] arr) {
int answer = 0;
int lt = Arrays.stream(arr).max().getAsInt();
int rt = Arrays.stream(arr).sum();
while (lt <= rt) {
int mid = (lt + rt) / 2;
if (count(arr, mid) <= m) {
answer = mid;
rt = mid - 1;
} else {
lt = mid + 1;
}
}
return answer;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
System.out.println(solution(n, m, arr));
}
public static int count(int[] arr, int capacity) {
int cnt = 1, sum = 0;
for (int x : arr) {
if (sum + x > capacity) {
cnt++;
sum = x;
} else {
sum += x;
}
}
return cnt;
}
}