Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b6ad7b3

Browse files
authored
Merge pull request ByteByteGoHq#33 from ongshunping/cpp-solutions-intervals
Add C++ solutions for Chapter 9 (Intervals)
2 parents 03f57e0 + 2279b81 commit b6ad7b3

File tree

3 files changed

+116
-0
lines changed

3 files changed

+116
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#include <vector>
2+
#include <algorithm>
3+
#include "ds/Interval.h"
4+
using ds::Interval;
5+
6+
/**
7+
* Definition of Interval:
8+
* struct Interval {
9+
* int start;
10+
* int end;
11+
* Interval(int start, int end) : start(start), end(end) {}
12+
* };
13+
*/
14+
15+
std::vector<Interval> identifyAllIntervalOverlaps(std::vector<Interval>& intervals1, std::vector<Interval>& intervals2) {
16+
std::vector<Interval> overlaps;
17+
int i = 0;
18+
int j = 0;
19+
while (i < intervals1.size() && j < intervals2.size()) {
20+
// Set A to the interval that starts first and B to the other
21+
// interval.
22+
Interval A, B;
23+
if (intervals1[i].start <= intervals2[j].start) {
24+
A = intervals1[i];
25+
B = intervals2[j];
26+
} else {
27+
A = intervals2[j];
28+
B = intervals1[i];
29+
}
30+
// If there's an overlap, add the overlap. Use `emplace_back`
31+
// to construct `Interval` in place, potentially avoiding an
32+
// extra copy compared to `push_back`.
33+
if (A.end >= B.start) {
34+
overlaps.emplace_back(B.start, std::min(A.end, B.end));
35+
}
36+
// Advance the pointer associated with the interval that ends
37+
// first.
38+
if (intervals1[i].end < intervals2[j].end) {
39+
i++;
40+
} else {
41+
j++;
42+
}
43+
}
44+
return overlaps;
45+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include <vector>
2+
#include <algorithm>
3+
#include "ds/Interval.h"
4+
using ds::Interval;
5+
6+
/**
7+
* Definition of Interval:
8+
* struct Interval {
9+
* int start;
10+
* int end;
11+
* Interval(int start, int end) : start(start), end(end) {}
12+
* };
13+
*/
14+
15+
int largestOverlapOfIntervals(std::vector<Interval>& intervals) {
16+
std::vector<std::pair<int, char>> points;
17+
for (Interval& interval : intervals) {
18+
points.emplace_back(interval.start, 'S');
19+
points.emplace_back(interval.end, 'E');
20+
}
21+
// Sort in chronological order. If multiple points occur at the same time,
22+
// ensure end points are prioritized before start points.
23+
std::sort(points.begin(), points.end());
24+
int activeIntervals = 0;
25+
int maxOverlaps = 0;
26+
for (auto& p : points) {
27+
int time = p.first;
28+
char pointType = p.second;
29+
if (pointType == 'S') {
30+
activeIntervals += 1;
31+
} else {
32+
activeIntervals -= 1;
33+
}
34+
maxOverlaps = std::max(maxOverlaps, activeIntervals);
35+
}
36+
return maxOverlaps;
37+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include <vector>
2+
#include <algorithm>
3+
#include "ds/Interval.h"
4+
using ds::Interval;
5+
6+
/**
7+
* Definition of Interval:
8+
* struct Interval {
9+
* int start;
10+
* int end;
11+
* Interval(int start, int end) : start(start), end(end) {}
12+
* };
13+
*/
14+
15+
std::vector<Interval> mergeOverlappingIntervals(std::vector<Interval>& intervals) {
16+
std::sort(intervals.begin(), intervals.end(), [](Interval& a, Interval& b) {
17+
return a.start < b.start;
18+
});
19+
std::vector<Interval> merged;
20+
merged.push_back(intervals[0]);
21+
for (int i = 1; i < intervals.size(); i++) {
22+
Interval& A = merged.back();
23+
Interval& B = intervals[i];
24+
// If A and B don't overlap, add B to the merged list.
25+
if (A.end < B.start) {
26+
merged.push_back(B);
27+
}
28+
// If they do overlap, merge A with B.
29+
else {
30+
A.end = std::max(A.end, B.end);
31+
}
32+
}
33+
return merged;
34+
}

0 commit comments

Comments
 (0)