Skip to content

Commit 4ecfaf6

Browse files
authored
Merge pull request ByteByteGoHq#17 from marttp/kotlin-intervals
Kotlin Chapter 9: Intervals
2 parents 600f156 + 13537f3 commit 4ecfaf6

File tree

3 files changed

+83
-0
lines changed

3 files changed

+83
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import ds.Interval
2+
3+
/*
4+
Definition of Interval:
5+
data class Interval(var start: Int, var end: Int)
6+
*/
7+
8+
fun identifyAllIntervalOverlaps(intervals1: List<Interval>, intervals2: List<Interval>): List<Interval> {
9+
val overlaps = mutableListOf<Interval>()
10+
var i = 0
11+
var j = 0
12+
while (i < intervals1.size && j < intervals2.size) {
13+
// Set A to the interval that starts first and B to the other
14+
// interval.
15+
val (A, B) = if (intervals1[i].start <= intervals2[j].start) {
16+
intervals1[i] to intervals2[j]
17+
} else {
18+
intervals2[j] to intervals1[i]
19+
}
20+
// If there's an overlap, add the overlap.
21+
if (A.end >= B.start) {
22+
overlaps.add(Interval(B.start, minOf(A.end, B.end)))
23+
}
24+
// Advance the pointer associated with the interval that ends
25+
// first.
26+
if (intervals1[i].end < intervals2[j].end) {
27+
i++
28+
} else {
29+
j++
30+
}
31+
}
32+
return overlaps
33+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import ds.Interval
2+
3+
/*
4+
Definition of Interval:
5+
data class Interval(var start: Int, var end: Int)
6+
*/
7+
8+
fun largestOverlapOfIntervals(intervals: List<Interval>): Int {
9+
val points = mutableListOf<Pair<Int, Char>>()
10+
for (interval in intervals) {
11+
points.add(interval.start to 'S')
12+
points.add(interval.end to 'E')
13+
}
14+
// Sort in chronological order. If multiple points occur at the same
15+
// time, ensure end points are prioritized before start points.
16+
points.sortWith(compareBy({ it.first }, { it.second }))
17+
var activeIntervals = 0
18+
var maxOverlaps = 0
19+
for ((time, pointType) in points) {
20+
if (pointType == 'S') {
21+
activeIntervals++
22+
} else {
23+
activeIntervals--
24+
}
25+
maxOverlaps = maxOf(maxOverlaps, activeIntervals)
26+
}
27+
return maxOverlaps
28+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import ds.Interval
2+
3+
/*
4+
Definition of Interval:
5+
data class Interval(var start: Int, var end: Int)
6+
*/
7+
8+
fun mergeOverlappingIntervals(intervals: List<Interval>): List<Interval> {
9+
val sortedIntervals = intervals.sortedBy { it.start }
10+
val merged = mutableListOf(sortedIntervals[0])
11+
for (B in sortedIntervals.subList(1, sortedIntervals.size)) {
12+
val A = merged.last()
13+
// If A and B don't overlap, add B to the merged list.
14+
if (A.end < B.start) {
15+
merged.add(B)
16+
// If they do overlap, merge A with B.
17+
} else {
18+
merged[merged.size - 1] = Interval(A.start, maxOf(A.end, B.end))
19+
}
20+
}
21+
return merged
22+
}

0 commit comments

Comments
 (0)