LeetCode 732. My Calendar III

Problem Statement


use std::collections::BTreeMap;

struct MyCalendarThree {
events: BTreeMap<i32, i32>
}


/**
* `&self` means the method takes an immutable reference.
* If you need a mutable reference, change it to `&mut self` instead.
*/
impl MyCalendarThree {

fn new() -> Self {
MyCalendarThree {
events: BTreeMap::new()
}
}

fn book(&mut self, start: i32, end: i32) -> i32 {
self.events.entry(start).or_insert(0);
self.events.entry(end).or_insert(0);
*self.events.get_mut(&start).unwrap() += 1;
*self.events.get_mut(&end).unwrap() -= 1;
let (mut count, mut ans) = (0, 0);
for (_k, v) in &self.events {
count += *v;
ans = std::cmp::max(count, ans);
}
return ans;
}
}

/**
* Your MyCalendarThree object will be instantiated and called as such:
* let obj = MyCalendarThree::new();
* let ret_1: i32 = obj.book(start, end);
*/