Keep and Share logo     Log In  |  Mobile View  |  Help  
 
Visiting
 
Select a Color
   
 












01.DS.02.08. Merge an Array With Overlapping Intervals.html -- 1.6 meg
 Merge an Array With Overlapping Intervals - Coderust: Hacking the Coding Interview
Log InJoin for free
Back To Module Home
Data Structures
0% completed
Further Reading
Mark Module as Completed
Ask a Question

Merge an Array With Overlapping Intervals

Merge overlapping intervals in an array of interval pairs.

Statement#

We’re given an array of interval pairs as input where each interval has a start and end timestamp. The input array is sorted by starting timestamps. Merge the overlapping intervals and return a new output array.

Example#

Consider the input array below. Intervals (1, 5), (3, 7), (4, 6), (6, 8) are overlapping, so they should be merged to one big interval (1, 8). Similarly, intervals (10, 12) and (12, 15) are also overlapping and should be merged to (10, 15).

garray1,53,74,66,8array210,1212,15array31,8array:e->array3:warray410,15array2:e->array4:w

Sample input#

intervals = [[1,3],[2,6],[8,10],[15,18]]

Expected output#

[[1,6],[8,10],[15,18]]

Try it yourself#

C++
Java
Python
JS
Ruby
Go
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <vector>
using namespace std;
typedef pair<int, int> Pair;
vector<Pair> MergeIntervals(vector<Pair>& intervals_list) {
    vector<Pair> t = {make_pair(NULL, NULL)};
    return t;
}
#
#include <iostream> #include <vector> using namespace std; typedef pair<int, int> Pair; vector<Pair> MergeIntervals(vector<Pair>& intervals_list) { vector<Pair> t = {make_pair(NULL, NULL)}; return t;
Enter to Rename, Shift+Enter to Preview
Test
Need Hint?
Save
Reset

Solution#

This problem can be solved in a simple linear scan algorithm. We know that the input is sorted by starting timestamps. Here is the approach we are following:

  • Using the given list of input intervals, we keep merged intervals in the output list.
  • For each interval in the input list:
    • If the input interval is overlapping with the last interval in the output list, then we merge these two intervals and update the last interval of the output list with the merged interval.
    • Otherwise, we add an input interval to the output list.

Below is a visual run-through of the above explanation:

The green interval in the input list represents the selected input interval. The last interval in the output list is green. Either it is updated by merging this interval and selected input interval or appending input interval.

Created with Fabric.js 3.6.6Input1,53,74,66,810,1211,15OutputInitial setup of the example
1 of 7
C++
Java
Python
JS
Ruby
Go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <vector>
using namespace std;
typedef pair<int, int> Pair;
vector<Pair> MergeIntervals(vector<Pair>& intervals_list) {
    if (intervals_list.size() == 0) {
        vector<Pair> t = {make_pair(NULL, NULL)};
        return t;
    }
    vector<Pair> result;
    // Adding pair in the result list
    result.push_back(
            make_pair(intervals_list[0].first, intervals_list[0].second));
    for (int i = 1; i < intervals_list.size(); i++) {
        // Getting the recent added pair in the result list
        Pair recent_added_pair = result.back();
        // Getting and initializing input pair
        int cur_start = intervals_list[i].first;
        int cur_end = intervals_list[i].second;
        // Getting and initializing recently added pair from result list
        int prev_end = recent_added_pair.second;
        // Overlapping condition
#
#include <iostream> #include <vector> using namespace std; typedef pair<int, int> Pair; vector<Pair> MergeIntervals(vector<Pair>& intervals_list) { if (intervals_list.size() == 0) { vector<Pair> t = {make_pair(NULL, NULL)};
Enter to Rename, Shift+Enter to Preview
Run
Save
Reset

Time complexity#

The time complexity of this solution is linear, O(n)O(n)O(n).

Space complexity#

The space complexity of this solution is linear, O(n)O(n)O(n).

Note: This is the worst case when there are non-overlapping elements in the array.

Back
Stock Buy Sell to Maximize Profit
Next
Find Pair With Given Sum in an Array
Mark as Completed
Report an Issue


Creation date: Oct 7, 2022 7:33pm     Last modified date: Oct 7, 2022 7:33pm   Last visit date: Dec 6, 2024 2:46pm
    Report Objectionable Content