Minimum Baskets with Distinct Fruit Types
Problem statement
Place every input fruit into a basket using the fewest possible baskets. Each basket may contain at most one fruit of each type.
For this exercise, assume the output is deterministic: scan fruits in input order and place the first occurrence of each type in basket 0, the second in basket 1, and so on. Within a basket, retain input order. Every fruit must occur exactly once in the result. Empty input returns an empty list.
Function
minimumFruitBaskets(fruits: String[]) → String[][]Examples
Example 1
fruits = ["a","b","c","a","b","c"]return = [["a","b","c"],["a","b","c"]]Two copies of each type require at least two baskets.
Example 2
fruits = ["pear","apple","pear"]return = [["pear","apple"],["pear"]]The second pear cannot share a basket with the first.
Example 3
fruits = []return = []No baskets are required.
Constraints
- For this exercise, assume
0 <= fruits.length <= 2000. - Every type is a nonempty lowercase English-letter string of length at most
20.