1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| 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; } };
|