2226. Maximum Candies Allocated to K Children Medium

@problem@discussion
#Array#Binary Search



1/**
2 * [2226] Maximum Candies Allocated to K Children
3 *
4 * You are given a 0-indexed integer array candies. Each element in the array denotes a pile of candies of size candies[i]. You can divide each pile into any number of sub piles, but you cannot merge two piles together.
5 * You are also given an integer k. You should allocate piles of candies to k children such that each child gets the same number of candies. Each child can take at most one pile of candies and some piles of candies may go unused.
6 * Return the maximum number of candies each child can get.
7 *  
8 * Example 1:
9 * 
10 * Input: candies = [5,8,6], k = 3
11 * Output: 5
12 * Explanation: We can divide candies[1] into 2 piles of size 5 and 3, and candies[2] into 2 piles of size 5 and 1. We now have five piles of candies of sizes 5, 5, 3, 5, and 1. We can allocate the 3 piles of size 5 to 3 children. It can be proven that each child cannot receive more than 5 candies.
13 * 
14 * Example 2:
15 * 
16 * Input: candies = [2,5], k = 11
17 * Output: 0
18 * Explanation: There are 11 children but only 7 candies in total, so it is impossible to ensure each child receives at least one candy. Thus, each child gets no candy and the answer is 0.
19 * 
20 *  
21 * Constraints:
22 * 
23 * 	1 <= candies.length <= 10^5
24 * 	1 <= candies[i] <= 10^7
25 * 	1 <= k <= 10^12
26 * 
27 */
28pub struct Solution {}
29
30// problem: https://leetcode.com/problems/maximum-candies-allocated-to-k-children/
31// discuss: https://leetcode.com/problems/maximum-candies-allocated-to-k-children/discuss/?currentPage=1&orderBy=most_votes&query=
32
33// submission codes start here
34
35impl Solution {
36    pub fn maximum_candies(candies: Vec<i32>, k: i64) -> i32 {
37        0
38    }
39}
40
41// submission codes end
42
43#[cfg(test)]
44mod tests {
45    use super::*;
46
47    #[test]
48    fn test_2226() {
49    }
50}
51


Back
© 2025 bowen.ge All Rights Reserved.