-
-
Save omo/e875569c64f3f2999813eeede2b4920a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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