Grokking the Coding Interview: Patterns for Coding Questions
Vote

0% completed

Introduction to Merge Intervals Pattern

You are given a list of meetings. Each meeting has a start time and an end time.

Your task is to combine all meetings that overlap.

[[1, 4], [2, 5], [7, 9], [8, 10]]

becomes

[[1, 5], [7, 10]]

A pair such as [1, 4] is called an interval. The first value is its start, and the second value is its end.

A simple solution compares every interval with every other interval. It merges an overlapping pair and repeats until no more intervals can be merged.

This solution performs at least O(N²) work. Repeating the comparisons can make it even slower.

.....

.....

.....

Like the course? Get enrolled and start learning!

Reading Progress

0%


Vote for new content