-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path05-my-calendar-i.rs
38 lines (34 loc) · 952 Bytes
/
05-my-calendar-i.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// https://leetcode.cn/problems/my-calendar-i/
struct MyCalendar {
map: std::collections::BTreeMap<i32, i32>,
}
/**
* `&self` means the method takes an immutable reference.
* If you need a mutable reference, change it to `&mut self` instead.
*/
impl MyCalendar {
fn new() -> Self {
Self { map: std::collections::BTreeMap::new() }
}
fn book(&mut self, start: i32, end: i32) -> bool {
let a = self.map.range(start..).next();
if let Some((&k, &v)) = a {
if k < end {
return false;
}
}
let b = self.map.range(..start+1).last();
if let Some((&k, &v)) = b {
if v > start {
return false;
}
}
self.map.insert(start, end);
true
}
}
/**
* Your MyCalendar object will be instantiated and called as such:
* let obj = MyCalendar::new();
* let ret_1: bool = obj.book(start, end);
*/