[JavaScript] 2226. Maximum Candies Allocated to K Children
\nWeekly Contest 287\n\n- Q1 answer\n  - https://leetcode.com/problems/minimum-number-of-operations-to-convert-time/discuss/1908839/JavaScript-2224.-Minimum-Num
\n**Weekly Contest 287**\n\n- Q1 answer\n  - https://leetcode.com/problems/minimum-number-of-operations-to-convert-time/discuss/1908839/JavaScript-2224.-Minimum-Number-of-Operations-to-Convert-Time\n- Q2 answer\n  - https://leetcode.com/problems/find-players-with-zero-or-one-losses/discuss/1908871/JavaScript-2225.-Find-Players-With-Zero-or-One-Losses\n- Q3 answer\n  - https://leetcode.com/problems/maximum-candies-allocated-to-k-children/discuss/1908890/JavaScript-2226.-Maximum-Candies-Allocated-to-K-Children\n  - **below**\n    - Similar to some other DP problems, but this is greedy\n    - **Less code** solution\n    - **More code - with comments** solution\n\n---\n\nHope it is simple to understand.\n\n---\n\n**Less code** solution\n\n```\nvar maximumCandies = function (candies, k) {\n    function ok(min_candy) {\n        let count = 0;\n        for (let candy of candies) count += Math.floor(candy / min_candy);\n        return count >= k;\n    }\n\n    let l = 0,\n        h = Math.max(...candies);\n\n    while (l <= h) {\n        let m = l + Math.trunc((h - l) / 2);\n        if (ok(m)) l = m + 1;\n        else h = m - 1;\n    }\n    return h;\n};\n```\n\n---\n\n**More code - with comments** solution\n\n```\nvar maximumCandies = function (cand, k) {\n    function is_good_number_of_candies(cmin) {\n        let count = 0;\n        for (let c of cand) {\n            count += Math.trunc(c / cmin); // how many mins (cmin) can we give from this pile (c)\n        }\n        return count >= k;\n    }\n\n    let sum = cand.reduce((sum, x) => sum + x, 0);\n    if (sum < k) return 0; // not enough, to give at least 1 candy\n\n    let lo = 1; // 1 is possible\n    let hi = cand.reduce((max, x) => Math.max(max, x), -Infinity); // max possible\n\n    // since # of candies are too large, try binary search\n    while (lo <= hi) {\n        let mid = lo + Math.trunc((hi - lo) / 2);\n        if (is_good_number_of_candies(mid)) {\n            lo = mid + 1; // mid is good (##), try next one\n        } else {\n            hi = mid - 1;\n        }\n    }\n    return lo - 1; // last good was mid, 1 below lo (## above)\n};\n```\n\n---\n
['JavaScript']