1333. Merge Source Trace
Two ticket counters each keep their customers in order of arrival time, given by the arrays first and second, both sorted in non-decreasing order. A supervisor merges the two lines into one sorted line. When the two customers at the head of each line have the same value, the one from first goes ahead. Return a string that records the source of every customer in merged order: the letter 'A' when the customer came from first and 'B' when it came from second. Either array may be empty, but at least one customer exists in total. Aim for O(n + m) time, where n and m are the lengths of the arrays.
Example 1
- Input:
- first = [1,3,5], second = [2,4,6]
- Output:
- "ABABAB"
- Explanation:
The values alternate between the two arrays, so the trace is ABABAB.
Example 2
- Input:
- first = [2,2], second = [2]
- Output:
- "AAB"
- Explanation:
The heads are equal, so first goes ahead twice before the 2 from second is taken, giving AAB.
Example 3
- Input:
- first = [], second = [5,6]
- Output:
- "BB"
- Explanation:
first is empty, so both customers come from second.
Constraints
0 ≤ first.length, second.length ≤ 500
first.length + second.length ≥ 1
Both arrays are sorted in non-decreasing order
-109 ≤ first[i], second[i] ≤ 109
How this problem is judged
- Answers
- Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
Expected complexity
- Time
- O(n + m)
- Space
- O(n + m)