3948. Lexicographically Maximum MEX Array
Description
You are given an integer array nums.
You want to construct an array result by repeatedly performing the following operation until nums becomes empty:
- Choose an integer
ksuch that1 <= k <= len(nums). - Compute the MEX of the first
kelements ofnums. - Append this MEX to
result. - Remove the first
kelements fromnums.
Return the lexicographically maximum array result that can be obtained after performing the operations.
The MEX of an array is the smallest non-negative integer not present in the array.
An array a is lexicographically greater than an array b if in the first position where a and b differ, array a has an element that is greater than the corresponding element in b. If the first min(a.length, b.length) elements do not differ, then the longer array is the lexicographically greater one.
Β
Example 1:
Input: nums = [0,1,0]
Output: [2,1]
Explanation:
- Take the first
k = 2elements[0, 1]which has MEX = 2. Currentresult = [2]. - Remaining array
[0]has MEX = 1. Thus, the finalresult = [2, 1].
Example 2:
Input: nums = [1,0,2]
Output: [3]
Explanation:
- Take the first
k = 3elements[1, 0, 2]which has MEX = 3. numsis now empty. Thus, the finalresult = [3].
Example 3:
Input: nums = [3,1]
Output: [0,0]
Explanation:βββββββ
- Take
k = 1, first element[3]has MEX = 0. Currentresult = [0]. - Remaining array
[1]has MEX = 0. Thus, the finalresult = [0, 0].
Β
Constraints:
1 <= nums.length <= 1050 <= nums[i] <= 105
Solutions
Solution 1
1 | |
1 | |
1 | |
1 | |