-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMergeOverlappingIntervals.cpp
More file actions
61 lines (51 loc) · 1.46 KB
/
MergeOverlappingIntervals.cpp
File metadata and controls
61 lines (51 loc) · 1.46 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
/*
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.
LINK: https://www.interviewbit.com/problems/merge-overlapping-intervals/
*/
/**
* 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 cmpfun(const Interval &a, const 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
int n = A.size();
vector<Interval> res;
if(n < 1) return res;
if(n == 1) {
res.push_back(Interval(A[0].start,A[0].end));
return res;
}
sort(A.begin(), A.end(), cmpfun);
int i = 0, a, b, c, d;
res.push_back(Interval(A[0].start,A[0].end));
for(int j = 0, i = 1; i<n; i++){
a = res[j].start;
b = res[j].end;
c = A[i].start;
d = A[i].end;
if(c > b){
res.push_back(Interval(c,d));
j++;
}else{
if(b<d){
res[j].end = d;
}
}
}
return res;
}