Skip to content

Instantly share code, notes, and snippets.

@omo

omo/Solution.kt Secret

Created August 3, 2022 12:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save omo/e875569c64f3f2999813eeede2b4920a to your computer and use it in GitHub Desktop.
Save omo/e875569c64f3f2999813eeede2b4920a to your computer and use it in GitHub Desktop.
class MyCalendar() {
data class Interval(val start: Int, val end: Int) {
fun overlaps(other: Interval) : Boolean = (
(start <= other.start && other.start < end) ||
(start < other.end && other.end <= end) ||
(start >= other.start && other.end >= end)
)
}
val intervals = mutableListOf<Interval>()
fun book(start: Int, end: Int): Boolean {
val toBook = Interval(start, end)
if (intervals.any{ it.overlaps(toBook)}) {
return false
}
intervals.add(toBook)
return true
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment