From 34d6a1901ecbeaf15690e4704a1242488b7a2906 Mon Sep 17 00:00:00 2001 From: jackfiled Date: Thu, 2 Jan 2025 15:37:25 +0800 Subject: [PATCH] 20250102 finished. --- src/problem/mod.rs | 2 ++ src/problem/p729_my_calendar_i.rs | 58 +++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 src/problem/p729_my_calendar_i.rs diff --git a/src/problem/mod.rs b/src/problem/mod.rs index 2a53ca0..ddbbab3 100644 --- a/src/problem/mod.rs +++ b/src/problem/mod.rs @@ -406,3 +406,5 @@ mod p1367_linked_list_in_binary_tree; mod p3219_minimum_cost_for_cutting_cake_ii; mod p3280_convert_date_to_binary; + +mod p729_my_calendar_i; diff --git a/src/problem/p729_my_calendar_i.rs b/src/problem/p729_my_calendar_i.rs new file mode 100644 index 0000000..e520d7e --- /dev/null +++ b/src/problem/p729_my_calendar_i.rs @@ -0,0 +1,58 @@ +/** + * [729] My Calendar I + */ +pub struct Solution {} + +// submission codes start here +use std::collections::BTreeMap; + +struct MyCalendar { + // (end, start) + end_time_map: BTreeMap, +} + +/** + * `&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 { + end_time_map: BTreeMap::new(), + } + } + + fn book(&mut self, start_time: i32, end_time: i32) -> bool { + if let Some((&end, &start)) = self.end_time_map.range(start_time + 1..).next() { + // 左闭右开区间 + if start < end_time { + return false; + } + } + + self.end_time_map.insert(end_time, start_time); + true + } +} + +/** + * Your MyCalendar object will be instantiated and called as such: + * let obj = MyCalendar::new(); + * let ret_1: bool = obj.book(startTime, endTime); + */ + +// submission codes end + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_729() { + let mut calendar = MyCalendar::new(); + + assert!(calendar.book(10, 20)); + assert!(!calendar.book(15, 25)); + assert!(calendar.book(20, 30)); + } +}