![JAR search and dependency download from the Maven repository](/logo.png)
g0701_0800.s0731_my_calendar_ii.MyCalendarTwo.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-kotlin Show documentation
Show all versions of leetcode-in-kotlin Show documentation
Kotlin-based LeetCode algorithm problem solutions, regularly updated
package g0701_0800.s0731_my_calendar_ii
// #Medium #Binary_Search #Design #Ordered_Set #Segment_Tree
// #2023_03_02_Time_371_ms_(100.00%)_Space_48.8_MB_(80.00%)
import java.util.TreeMap
@Suppress("NAME_SHADOWING")
class MyCalendarTwo {
private val map: TreeMap = TreeMap()
private val overlap: TreeMap = TreeMap()
fun book(start: Int, end: Int): Boolean {
var start = start
var end = end
val ol = overlap.lowerKey(end)
if (ol == null || overlap[ol]!! <= start) {
while (true) {
val lower = map.lowerKey(end)
if (lower == null || map[lower]!! <= start) {
break
}
overlap[start.coerceAtLeast(lower)] = end.coerceAtMost(map[lower]!!)
start = start.coerceAtMost(lower)
end = end.coerceAtLeast(map[lower]!!)
map.remove(lower)
}
map[start] = end
return true
}
return false
}
}
/*
* Your MyCalendarTwo object will be instantiated and called as such:
* var obj = MyCalendarTwo()
* var param_1 = obj.book(start,end)
*/
© 2015 - 2025 Weber Informatics LLC | Privacy Policy