Merge and Intervals
What is this?
Once things are already in order, problems get much easier. Merging two sorted lists is like shuffling two pre-sorted stacks of cards into one, always taking the smaller top card. And once time slots are lined up by start time, you can spot any overlap with a single left-to-right pass instead of comparing every pair.
๐ก Fun fact: The "merge" step here is the exact same move at the heart of merge sort, and it powers how databases combine sorted files too big to fit in memory.
๐ The 2 problems in this chapter are free. Sign in with Google or Microsoft to start solving.
Core idea: Two recurring payoffs of sorted order. First, the merge step from Merge Sort stands on its own: combine two already-sorted sequences with a linear pass โ and when one has spare room, fill it from the back to avoid overwriting. Second, once items are sorted by start time, overlaps become a simple left-to-right sweep: anything that begins before the previous one ends is a conflict.
The pattern
When inputs are already sorted, you rarely need to sort again โ you walk them in order. Merging two sorted arrays is a two-pointer scan taking the smaller front each step; the only twist for an in-place merge is to write from the largest end backward so you never clobber a value you still need to read. For intervals, sorting by start lines events up on a timeline: scan once, and the moment a new interval's start falls before the running end, you have an overlap. Sorting turns "compare every pair" into "compare each to its neighbour."
The problems
- Merge Sorted Array โ merge two sorted arrays into the first, which has trailing space. Walk both from the back, writing the larger element into the tail; O(m+n) time, O(1) extra space, no risk of overwriting unread values.
- Meeting Rooms โ given meeting intervals, decide if a person can attend all of them. Sort by start time, then sweep: if any meeting begins before the previous one ends, they overlap and the answer is false. O(n log n) for the sort, O(1) afterward.
Key takeaways
- Merging two sorted runs is linear โ the same step that powers Merge Sort.
- Merge in place from the back when the destination has trailing room, so writes never overwrite unread reads.
- Sort intervals by start, then a single sweep detects every overlap by comparing each start to the running end.
- Why it matters: "sort, then sweep" is one of the most reused interview patterns โ the foundation for harder interval problems like merging and meeting-rooms-II.
Order: Merge Sorted Array โ Meeting Rooms.
Merge Sorted Array
Core idea: You have two sorted arrays and the result must live inside the first one, which already has trailing blank space. Instead of fighting for room at the front, fill from the back: compare the largest remaining element of each array and drop it into the last empty slot. The write pointer always sits on or behind the slack space, so it can never clobber a value you haven't read yet. One backward pass, O(m + n) time, O(1) extra space.
The problem, rephrased
You're handed two sorted guest lists for a single elevator ride. nums1 is the elevator's manifest: it has room for everybody โ its first m slots hold real guests (sorted), and the last n slots are empty placeholders (written as 0s) reserved as scratch space. nums2 holds n more guests, also sorted. Merge nums2's guests into nums1 so that, when the doors close, the whole manifest is sorted in non-decreasing order. You do not return a new list โ you rearrange nums1 itself.
This is LeetCode 88 โ Merge Sorted Array.
| Input | Means | Output (in nums1) |
|---|---|---|
nums1 = [1,2,3,0,0,0], m = 3, nums2 = [2,5,6], n = 3 |
three real + three placeholders, merge three more | [1,2,2,3,5,6] |
nums1 = [1], m = 1, nums2 = [], n = 0 |
nothing to merge | [1] |
nums1 = [0], m = 0, nums2 = [1], n = 1 |
nums1 is all scratch space |
[1] |
The function returns nothing โ the answer is whatever ends up in nums1.
Sign in to continue reading
The rest of this lesson is available with a free account. Signing in with Google or Microsoft is free.
Sign in to read the full lesson