729. My Calendar I Medium

@problem@discussion
#Binary Search#Design#Segment Tree#Ordered Set



1/**
2 * [729] My Calendar I
3 *
4 * You are implementing a program to use as your calendar. We can add a new event if adding the event will not cause a double booking.
5 * A double booking happens when two events have some non-empty intersection (i.e., some moment is common to both events.).
6 * The event can be represented as a pair of integers start and end that represents a booking on the half-open interval [start, end), the range of real numbers x such that start <= x < end.
7 * Implement the MyCalendar class:
8 * 
9 * 	MyCalendar() Initializes the calendar object.
10 * 	boolean book(int start, int end) Returns true if the event can be added to the calendar successfully without causing a double booking. Otherwise, return false and do not add the event to the calendar.
11 * 
12 *  
13 * Example 1:
14 * 
15 * Input
16 * ["MyCalendar", "book", "book", "book"]
17 * [[], [10, 20], [15, 25], [20, 30]]
18 * Output
19 * [null, true, false, true]
20 * Explanation
21 * MyCalendar myCalendar = new MyCalendar();
22 * myCalendar.book(10, 20); // return True
23 * myCalendar.book(15, 25); // return False, It can not be booked because time 15 is already booked by another event.
24 * myCalendar.book(20, 30); // return True, The event can be booked, as the first event takes every time less than 20, but not including 20.
25 *  
26 * Constraints:
27 * 
28 * 	0 <= start < end <= 10^9
29 * 	At most 1000 calls will be made to book.
30 * 
31 */
32pub struct Solution {}
33
34// problem: https://leetcode.com/problems/my-calendar-i/
35// discuss: https://leetcode.com/problems/my-calendar-i/discuss/?currentPage=1&orderBy=most_votes&query=
36
37// submission codes start here
38
39struct MyCalendar {
40        vec![]
41    }
42
43
44/** 
45 * `&self` means the method takes an immutable reference.
46 * If you need a mutable reference, change it to `&mut self` instead.
47 */
48impl MyCalendar {
49
50    fn new() -> Self {
51        
52    }
53    
54    fn book(&self, start: i32, end: i32) -> bool {
55        
56    }
57}
58
59/**
60 * Your MyCalendar object will be instantiated and called as such:
61 * let obj = MyCalendar::new();
62 * let ret_1: bool = obj.book(start, end);
63 */
64
65// submission codes end
66
67#[cfg(test)]
68mod tests {
69    use super::*;
70
71    #[test]
72    fn test_729() {
73    }
74}
75


Back
© 2025 bowen.ge All Rights Reserved.