You are given a list of integers data_packets. The algorithm repeatedly performs the following until data_packets becomes empty:
- Choose an integer
ksuch that1 ≤ k ≤ length(data_packets). - Compute the MEX (Minimum Excludant) of the first
kelements (i.e., elements at indices0throughk-1, using 0-based indexing). - The MEX of a set of non-negative integers is the smallest non-negative integer not present in the set.
- Example:
MEX({1,2,3}) = 0,MEX({0,1,2,4}) = 3.
- Example:
- Append this MEX to an array
result. - Remove the first
kelements fromdata_packets.
Your task is to find the lexicographically maximum array result that can be obtained.
Note:
- An array
xis lexicographically greater than arrayyif at the first index where they differ,x[i] > y[i], or ifyis a prefix ofx(all indices are 0-based).- Example:
[3,2,1] > [3,1,5](since2 > 1at index1). - Example:
[1,2,3,4] > [1,2,3](since the latter is a prefix).
- Example:
Explanation
Given data_packets = [0,1,1,0], one of the optimal ways to make the array result lexicographically maximum is as follows -
- Take
k = 2, the MEX of elements at indices0and1ofdata_packetsis2.
Next: After first k elements (index 0 and 1 for k=2 ) are removed.data_packets = [1,0]andresult = [2]. - Again, choose
k = 2, the MEX of elements at indices0and1ofdata_packetsis2.
So after again removing first 2 elements,data_packets = []andresult = [2,2].
data_packets is now empty, and the answer is [2,2].
Function Description
Complete the function getMaxArray.
Input (only):
List<Integer> data_packets: the streaming data packets.
Returns:
List<Integer>: the lexicographically maximum arrayresultobtainable using the algorithm.
Constraints
1 ≤ length(data_packets) ≤ 1050 ≤ data_packets[i] ≤ length(data_packets)(for all valid 0-based indicesi)
Examples
Example 0
Input: data_packets = [2, 2, 3, 4, 0, 1, 2, 0]
Output: [5, 1]
Explanation
- Take
k = 6, the MEX of indices0..5(values[2,2,3,4,0,1]) is5. Sodata_packets = [2, 0]andresult = [5]. - Take
k = 2, the MEX of indices0..1(values[2,0]) is1. Sodata_packets = []andresult = [5, 1].
Example 1
Input: data_packets = [0, 1, 2, 3, 4, 6]
Output: [5, 0]
Explanation
- Take
k = 5, the MEX of indices0..4(values[0,1,2,3,4]) is5. Sodata_packets = [6]andresult = [5]. - Take
k = 1, the MEX of index0(value[6]) is0. Sodata_packets = []andresult = [5, 0].
Example 2
Input: data_packets = [1,2,3]
Output: [0,0,0]
Explanation: Any prefix that does not contain 0 has MEX 0, so taking k = 1 repeatedly yields the lexicographically maximum result.
Example 3
Input: data_packets = [0,2,1,0,2]
Output: [3,1,0]
One optimal segmentation: take k = 3 (MEX of indices 0..2, values [0,2,1], is 3), then k = 1 (MEX of [0] is 1), then k = 1 (MEX of [2] is 0).
Example 4
Input: data_packets = [0,0,1,2]
Output: [3]
Taking the whole list at once (choose k = 4, indices 0..3) yields MEX 3, maximizing the first element of result.