Merge two sorted logs

EasyAbout 15 minProblem 1 of 2 in Two pointersDraft

The problem

Two servers each write a log of event times, in seconds, sorted from earliest to latest.

Return one list with every time from both logs, sorted from earliest to latest. Keep duplicates: two events at the same second are two events.

  • 0 ≤ a.length, b.length ≤ 200,000
  • a and b are each sorted, non-decreasing

Examples

  1. mergeLogs([1, 4, 9], [2, 3, 10, 11])returns [1, 2, 3, 4, 9, 10, 11]
  2. mergeLogs([5, 5], [5])returns [5, 5, 5]
  3. mergeLogs([], [7])returns [7]

Hints

Hint 1 of 2

Keep a position in each log. The next time in the merged log is the smaller of the two times at those positions.

Hint 2 of 2

When one log runs out, everything left in the other log comes next, in its order.

Editorial

Opens once you solve the problem, or after three checked attempts.

Your solution

Tab indents. Press Esc, then Tab, to leave the editor. Ctrl+Enter runs the examples.

Esc
↑ ↓ to move↵ to open