- Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy path_1891.java
54 lines (52 loc) · 1.66 KB
/
_1891.java
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
47
48
49
50
51
52
53
54
packagecom.fishercoder.solutions.secondthousand;
importjava.util.Arrays;
publicclass_1891 {
publicstaticclassSolution1 {
/*
* My completely original solution on 1/27/2022.
*/
publicintmaxLength(int[] ribbons, intk) {
longsum = 0L;
intmax = ribbons[0];
for (intnum : ribbons) {
sum += num;
max = Math.max(max, num);
}
if (sum < k) {
return0;
} elseif (sum == k) {
return1;
} else {
Arrays.sort(ribbons);
intleft = 1;
intright = max;
intans = 1;
while (left < right) {
intmid = left + (right - left) / 2;
intcount = 0;
for (inti = ribbons.length - 1; i >= 0; i--) {
count += ribbons[i] / mid;
if (count >= k) {
ans = Math.max(ans, mid);
break;
}
}
if (count < k) {
right = mid - 1;
} else {
left = mid + 1;
}
}
intcount = 0;
for (inti = ribbons.length - 1; i >= 0; i--) {
count += ribbons[i] / left;
if (count >= k) {
ans = Math.max(ans, left);
returnans;
}
}
returnans;
}
}
}
}