-
Notifications
You must be signed in to change notification settings - Fork 9
/
MergeIntervals.cpp
49 lines (44 loc) · 1.36 KB
/
MergeIntervals.cpp
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
/*
Merge Overlapping Intervals
Given a collection of intervals, merge all overlapping intervals.
For example: Given [1,3],[2,6],[8,10],[15,18], return [1,6],[8,10],[15,18]. Make sure the returned intervals are sorted.
*/
// sort using comparator and then if end of one interval is greater then start of next merge them else add the interval to res
/**
* Definition for an interval.
* struct Interval {
* int start;
* int end;
* Interval() : start(0), end(0) {}
* Interval(int s, int e) : start(s), end(e) {}
* };
*/
bool comp(Interval a, Interval b)
{
return (a.start < b.start);
}
vector<Interval> Solution::merge(vector<Interval> &A)
{
// Do not write main() function.
// Do not read input, instead use the arguments to the function.
// Do not print the output, instead return values as specified
// Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details
sort(A.begin(), A.end(), comp);
//int idx=0;
vector<Interval> res;
res.emplace_back(A[0]);
for (int i = 1; i < A.size(); i++)
{
if (res.back().end >= A[i].start)
{
res.back().end = max(res.back().end, A[i].end);
// res.back().start=min(res.back().start, A[i].start);
}
else
{
res.emplace_back(A[i]);
//idx++;
}
}
return res;
}