A record store receives n vinyl records for its back catalog; each record is stamped with an integer catalog code, and the same code can appear on more than one record when a title was pressed multiple times. The stockroom's numbered shelves, starting at shelf 1, follow one strict rule: a single shelf may never hold two records that share a catalog code. Copies of the same code are always shelved in encounter order -- the first copy of a code goes on shelf 1, the second copy of that same code goes on shelf 2, the third goes on shelf 3, and so on, so a code that appears c times ends up occupying shelves 1 through c (one copy per shelf) and never touches shelf c + 1 or beyond.
Given the codes for all n records, determine how many shelves this rule requires in total, and, for every shelf from 1 up to that total, which catalog codes sit on it.
Example 1
Input
7 1 3 4 1 2 3 1
Expected
3 4 1 2 3 4 2 1 3 1 1
Explanation
The code 1 appears 3 times (its copies go to shelves 1, 2, and 3), code 3 appears twice (shelves 1 and 2), and codes 4 and 2 each appear once (shelf 1 only). The largest copy count is 3, so 3 shelves are needed. Shelf 1 holds every code that appears at least once -- 1, 2, 3, 4 -- shelf 2 holds every code that appears at least twice -- 1, 3 -- and shelf 3 holds only the code that appears at least 3 times -- 1.
Example 2
Input
4 5 2 9 1
Expected
1 4 1 2 5 9
Explanation
All four codes are distinct, so every code appears exactly once and only ever needs shelf 1. One shelf holds all four codes, listed in increasing order: 1 2 5 9.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →