LeetCode 731. My Calendar II

Problem Statement


use std::collections::BTreeMap;

struct MyCalendarTwo {
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 MyCalendarTwo {

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

fn book(&mut self, start: i32, end: i32) -> bool {
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 sum = 0;
for (_k, v) in &self.events {
sum += *v;
if sum == 3 {
*self.events.get_mut(&start).unwrap() -= 1;
*self.events.get_mut(&end).unwrap() += 1;
return false;
}
}

return true;
}
}