Minimum Number of Groups to Create a Valid Assignment

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 in the same box must have the same value. If you have more than one ball with the same number, you may put them in different boxes.
  • The biggest box can only have one more ball than the smallest box.

Return the fewest number of boxes needed to sort these balls while following these rules.

Example 1
Inputballs = [3,2,3,2,3]
Output2
The balls can be sorted into boxes [3,3,3] and [2,2], whose size difference does not exceed one.
Example 2
Inputballs = [10,10,10,3,1,1]
Output4
You cannot use fewer than four boxes while still following the rules, since putting all three balls numbered 10 in one box would make the box sizes differ by more than one.

Constraints

  • 1 <= nums.length <= 10^5
  • 1 <= nums[i] <= 10^9

Asked at 1 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate