DIY: Merge Intervals

Solve the interview question "Merge Intervals" in this lesson.

Problem statement

You are given a list of intervals in the form of start and end times. Your task is to merge as many intervals as possible. Two intervals can be merged if they are overlapping (one begins before the other ends) or adjacent (one starts exactly when the other ends). The output is the resultant list of intervals.

Input

The input is a list of lists. The nested lists contain two integers representing the starting and ending points of the interval. The following is an example input:

{{1, 4}, {2, 5}, {6, 7}, {7, 10}, {11, 12}}

Output

The output is the list of merged intervals. The following is an example output:

{{1, 5}, {6, 10}, {11, 12}}

Level up your interview prep. Join Educative to access 80+ hands-on prep courses.