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 valuesort(v.begin(), v.end());//if no elementsif (v.size() == 0)return res;//stackstack<pair<int, int>> st;//insert first pair into the stackst.push({v[0][0], v[0][1]});for (int i = 1; i < v.size(); i++){//top of stackpair<int, int> p = st.top();//if start is less than stack top//second then add into the stackif (p.second < v[i][0])st.push({v[i][0], v[i][1]});else{//update end intervalif (p.second < v[i][1])p.second = v[i][1];//pop from stackst.pop();//push into the stackst.push(p);}}while (!st.empty()){pair<int, int> p = st.top();st.pop();vector<int> x;x.push_back(p.first);x.push_back(p.second);res.push_back(x);}//reverse the resultreverse(res.begin(), res.end());return res;}int main(){vector<vector<int>> intervals = {{1, 3}, {5, 8},{4, 10}, {20, 25}};intervals = merge(intervals);cout << "[";for (int i = 0; i < intervals.size(); i++){cout << "[";cout << intervals[i][0] << "," << intervals[i][1];cout << "]";if (i != intervals.size() - 1)cout << ",";}cout << "]";}
No comments:
Post a Comment