forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_1891.java
54 lines (52 loc) · 1.65 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
package com.fishercoder.solutions;
import java.util.Arrays;
public class _1891 {
public static class Solution1 {
/**
* My completely original solution on 1/27/2022.
*/
public int maxLength(int[] ribbons, int k) {
long sum = 0l;
int max = ribbons[0];
for (int num : ribbons) {
sum += num;
max = Math.max(max, num);
}
if (sum < k) {
return 0;
} else if (sum == k) {
return 1;
} else {
Arrays.sort(ribbons);
int left = 1;
int right = max;
int ans = 1;
while (left < right) {
int mid = left + (right - left) / 2;
int count = 0;
for (int i = 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;
}
}
int count = 0;
for (int i = ribbons.length - 1; i >= 0; i--) {
count += ribbons[i] / left;
if (count >= k) {
ans = Math.max(ans, left);
return ans;
}
}
return ans;
}
}
}
}