-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInsert_Interval.cpp
More file actions
71 lines (57 loc) · 1.81 KB
/
Insert_Interval.cpp
File metadata and controls
71 lines (57 loc) · 1.81 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*
Problem Statement Given a list of non-overlapping intervals sorted by their start time, insert a given interval at the correct position and merge all necessary intervals to produce a list that has only mutually exclusive intervals.
Example:
Input: Intervals=[[1,3], [5,7], [8,12]], New Interval=[4,6]
Output: [[1,3], [4,7], [8,12]]
Explanation: After insertion, since [4,6] overlaps with [5,7], we merged them into one [4,7].
*/
/*Naive Approach goes like take the new interval put it into the array of intervals at the last, then sort the intervals according to start time, and merge accordingly
Time Complexity : O(N logn)
Space Compexity : O(1)
Efficient idea would be to make use of sorting property given int he problem statement which reduces time complexity to O(N)
*/
#include<bits/stdc++.h>
#include<iostream>
using namespace std;
struct Interval
{
int s,e;
};
vector<Interval> Mergeintervals(Interval arr[], Interval NewInterval, int n)
{
if(n==0)
{
vector<Interval> NewInterval;
}
vector<Interval> MergedIntervals;
int i=0;
while(i<n && arr[i].e<NewInterval.s)
{
MergedIntervals.push_back(arr[i]);
i+=1;
}
while(i<n && arr[i].s<=NewInterval.e)
{
NewInterval.s=min(NewInterval.s,arr[i].s);
NewInterval.e=max(NewInterval.e,arr[i].e);
i++;
}
MergedIntervals.push_back(NewInterval);
while(i<n)
{
MergedIntervals.push_back(arr[i]);
i+=1;
}
return MergedIntervals;
}
int main()
{
Interval arr[]={{1,3},{5,7},{8,12}};
Interval NewInterval={4,10};
int n=sizeof(arr)/sizeof(arr[0]);
vector<Interval> result;
for(auto result : Mergeintervals(arr,NewInterval,n))
{
cout<<"["<<result.s<<","<<result.e<<"]";
}
}