Merge the overlapping Intervals

Given a list of possibly overlapping intervals, return a new list of intervals where all overlapping intervals have been merged.

The input list is not necessarily ordered in any way.

For example, given [(1, 3), (5, 8), (4, 10), (20, 25)], you should return [(1, 3), (4, 10), (20, 25)].

Example:

Input:  arr={{1,3},{5,8},{4,10},{20,25}}
Output: [[1,3],[4,10],[10,25]]

Approach

C++

#include <bits/stdc++.h>
using namespace std;

vector<vector<int>> merge(vector<vector<int>> &v)
{
    vector<vector<int>> res;

    //sort the 2d array according to
    //firt value
    sort(v.begin(), v.end());

    //if no elements
    if (v.size() == 0)
        return res;

    //stack
    stack<pair<intint>> st;

    //insert first pair into the stack
    st.push({v[0][0]v[0][1]});
    for (int i = 1i < v.size(); i++)
    {

        //top of stack
        pair<intintp = st.top();

        //if start is less than stack top
        //second then add into the stack
        if (p.second < v[i][0])
            st.push({v[i][0]v[i][1]});
        else
        {
            //update end interval
            if (p.second < v[i][1])
                p.second = v[i][1];

            //pop from stack
            st.pop();
            //push into the stack
            st.push(p);
        }
    }
    while (!st.empty())
    {
        pair<intintp = st.top();
        st.pop();
        vector<intx;
        x.push_back(p.first);
        x.push_back(p.second);
        res.push_back(x);
    }

    //reverse the result
    reverse(res.begin(), res.end());
    return res;
}
int main()
{
    vector<vector<int>> intervals = {{13}, {58}, 
                                     {410}, {2025}};
    intervals = merge(intervals);

    cout << "[";
    for (int i = 0i < intervals.size(); i++)
    {
        cout << "[";
        cout << intervals[i][0] << "," << intervals[i][1];
        cout << "]";
        if (i != intervals.size() - 1)
            cout << ",";
    }
    cout << "]";
}


No comments:

Post a Comment