Minimum Number of Groups to Create a Valid Assignment
Real-world version: Fewest Crates to Sort the BearingsThe statement here is our own: the same inputs, outputs and technique.
A tray of bearings is given as balls, where balls[i] is the grade of bearing i.
Every bearing must go into a crate, and a crate may only hold bearings of a single grade. The packing is tidy when any two crates differ in count by at most one.
Return the fewest crates a tidy packing can use.
Examples
Example 1
- Input
- balls = [5, 5, 5, 5, 5, 6]
- Output
- 4
Grade 6 appears once, so every crate must hold one or two bearings. The five grade 5 bearings need three crates at that size, and the grade 6 bearing needs one of its own.
Example 2
- Input
- balls = [1, 1, 1, 2]
- Output
- 3
Grade 2 appears once, so every crate must hold one or two bearings. Three crates of one grade-1 bearing each, plus one for the grade 2, gives four.
Example 3
- Input
- balls = [7, 7, 7]
- Output
- 1
All three bearings share a grade, so a single crate holds them and no other crate exists to differ from.
Constraints
- 1 <= balls.length <= 10^5
- 1 <= balls[i] <= 10^9
The signature
The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
def fewest_crates(balls: list[int]) -> int:public int fewestCrates(int[] balls)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 11 test cases: 11 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's
More on the same topics
- Make Array Zero by Subtracting Equal AmountsReal-world version: Rounds Needed to Clear Every ReadingEasy
- Maximum Unique Subarray Sum After DeletionReal-world version: Keeping the Best Run of Different ChipsEasy
- Task SchedulerReal-world version: Press Die CooldownMedium
- Maximum Palindromes After OperationsReal-world version: Noticeboard Word StripsMedium