2221-2230¶
class Solution {
public:
#define ll long long
bool check(const vector<int> &nums, int x, ll k) {
ll sum = 0;
for (auto it: nums)sum += it / x;
return sum >= k ? true : false;
}
int maximumCandies(vector<int> &candies, long long k) {
int l = 0, r = *max_element(candies.begin(), candies.end()), mid = 0;
while (l < r) {
mid = l + r + 1 >> 1;
if (check(candies, mid, k)) l = mid;
else r = mid - 1;
}
return l;
}
};