53. OA - Build Maximum MEX Array from Streaming Packet Segmentation

Build Maximum MEX Array from Streaming Packet Segmentation
Amazon developers are designing an algorithm to optimize segmentation of streaming data packets.

You are given a list of integers data_packets. The algorithm repeatedly performs the following until data_packets becomes empty:

  • Choose an integer k such that 1 ≤ k ≤ length(data_packets).
  • Compute the MEX (Minimum Excludant) of the first k elements (i.e., elements at indices 0 through k-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.
  • Append this MEX to an array result.
  • Remove the first k elements from data_packets.

Your task is to find the lexicographically maximum array result that can be obtained.

Note:

  • An array x is lexicographically greater than array y if at the first index where they differ, x[i] > y[i], or if y is a prefix of x (all indices are 0-based).
    • Example: [3,2,1] > [3,1,5] (since 2 > 1 at index 1).
    • Example: [1,2,3,4] > [1,2,3] (since the latter is a prefix).

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 indices 0 and 1 of data_packets is 2.
    Next: After first k elements (index 0 and 1 for k=2 ) are removed. data_packets = [1,0] and result = [2].
  • Again, choose k = 2, the MEX of elements at indices 0 and 1 of data_packets is 2.
    So after again removing first 2 elements, data_packets = [] and result = [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 array result obtainable using the algorithm.

Constraints

  • 1 ≤ length(data_packets) ≤ 105
  • 0 ≤ data_packets[i] ≤ length(data_packets) (for all valid 0-based indices i)

Examples

Example 0

Input: data_packets = [2, 2, 3, 4, 0, 1, 2, 0]

Output: [5, 1]

Explanation

  • Take k = 6, the MEX of indices 0..5 (values [2,2,3,4,0,1]) is 5. So data_packets = [2, 0] and result = [5].
  • Take k = 2, the MEX of indices 0..1 (values [2,0]) is 1. So data_packets = [] and result = [5, 1].

Example 1

Input: data_packets = [0, 1, 2, 3, 4, 6]

Output: [5, 0]

Explanation

  • Take k = 5, the MEX of indices 0..4 (values [0,1,2,3,4]) is 5. So data_packets = [6] and result = [5].
  • Take k = 1, the MEX of index 0 (value [6]) is 0. So data_packets = [] and result = [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.





Please use Laptop/Desktop or any other large screen to add/edit code.