Maximum Candies Allocated to K Children LeetCode solution in Java

1
class Solution {
2
public boolean canSplit(int[] candies, long k, long mid) {
3
long split = 0;
4
for (int i = 0; i < candies.length; ++i) {
5
split += candies[i] / mid;
6
}
7
if (split >= k) return true;
8
else return false;
9
}
10

11
public int maximumCandies(int[] candies, long k) {
12
long sum = 0;
13
for (int i = 0; i < candies.length; ++i) {
14
sum += candies[i];
15
}
16
long start = 1, end = sum;
17
long ans = 0;
18
while (start <= end) {
19
long mid = (start + end) / 2;
20
if (canSplit(candies, k, mid)) {
21
ans = mid;
22
start = mid + 1;
23
} else {
24
end = mid - 1;
25
}
26
}
27
return (int) ans;
28
}
29
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0