-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay10.cpp
33 lines (30 loc) · 1.05 KB
/
Day10.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
/**
* Definition for an interval.
* struct Interval {
* int start;
* int end;
* Interval() : start(0), end(0) {}
* Interval(int s, int e) : start(s), end(e) {}
* };
*/
vector<Interval> Solution::insert(vector<Interval> &intervals, Interval newInterval) {
// 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
intervals.push_back(newInterval);
sort(intervals.begin(), intervals.end(), [](const Interval& a, const Interval& b) {
return a.start < b.start;
});
vector<Interval> merged;
merged.push_back(intervals[0]);
for(int i = 1; i < intervals.size(); i++) {
if(merged.back().end >= intervals[i].start) {
merged.back().end = max(merged.back().end, intervals[i].end);
}
else {
merged.push_back(intervals[i]);
}
}
return merged;
}