Lexicographically Maximum MEX Array
Real-world version: Largest Group Gap ReportThe statement here is our own: the same inputs, outputs and technique.
A conveyor carries bins in a fixed order. Bin i holds a token stamped with the whole number nums[i].
The MEX of a collection of tokens is the least non-negative integer that no token in the collection carries. So a collection with no 0 has MEX 0, and a collection holding 0 and 1 but no 2 has MEX 2.
You cut the conveyor into consecutive groups: every bin lands in exactly one group, no group is empty, and the groups keep conveyor order. The report of a cut is the list of the MEX values of its groups, in the same order as the groups.
Return the largest report under this comparison: compare two reports at the first position where they differ, and the one with the bigger value there is larger; if neither differs anywhere and one is shorter, the longer report is larger. Any number of groups from one up to nums.length is allowed.
Examples
Example 1
- Input
- nums = [5,0,3,1,2,0]
- Output
- [4,1]
Cutting after the fifth bin gives the groups `[5,0,3,1,2]` and `[0]`. The first group misses 4 and carries everything below it, so its MEX is 4; the second group carries 0 and misses 1, so its MEX is 1.
Example 2
- Input
- nums = [0,2,1,0,0]
- Output
- [3,1,1]
The groups are `[0,2,1]`, `[0]` and `[0]`, with MEX values 3, 1 and 1. Every bin sits in exactly one group and the order is kept.
Example 3
- Input
- nums = [7,7]
- Output
- [0,0]
No bin carries 0, so any group at all has MEX 0. Splitting into two single-bin groups gives a report of two zeros, which beats the shorter report `[0]` from a single group.
Constraints
- 1 <= nums.length <= 10^5
- 0 <= nums[i] <= 10^5
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 maximum_m_e_x(nums: list[int]) -> list[int]:public int[] maximumMEX(int[] nums)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 10 test cases: 10 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