You are given a collection of numbered balls and instructed to sort them into boxes for a nearly balanced distribution. There are two rules you must follow:
Balls with the same box must have the same value. But, if you have more than one ball with the same number, you can put them in different boxes.
The biggest box can only have one more ball than the smallest box.
Return the fewest number of boxes to sort these balls following these rules.
Input: balls =[3,2,3,2,3]Output: 2Explanation:
We can sort `balls` into boxes as follows:*`[3,3,3]`*`[2,2]`The size difference between the two boxes doesn't exceed one.
Input: balls =[10,10,10,3,1,1]Output: 4Explanation:
We can sort `balls` into boxes as follows:*`[10]`*`[10,10]`*`[3]`*`[1,1]`You can't use fewer than four boxes while still following the rules. For
example, putting all three balls numbered 10in one box would break the rule
about the maximum size difference between boxes.
For each value, split its balls into groups of size ⌈count/boxes⌉, so that the largest and smallest box differ by at most one. The minimum number of boxes is the maximum frequency among all values.