731. My Calendar II Medium
1/**
2 * [731] My Calendar II
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 triple booking.
5 * A triple booking happens when three events have some non-empty intersection (i.e., some moment is common to all the three 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 MyCalendarTwo class:
8 *
9 * MyCalendarTwo() 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 triple booking. Otherwise, return false and do not add the event to the calendar.
11 *
12 *
13 * Example 1:
14 *
15 * Input
16 * ["MyCalendarTwo", "book", "book", "book", "book", "book", "book"]
17 * [[], [10, 20], [50, 60], [10, 40], [5, 15], [5, 10], [25, 55]]
18 * Output
19 * [null, true, true, true, false, true, true]
20 * Explanation
21 * MyCalendarTwo myCalendarTwo = new MyCalendarTwo();
22 * myCalendarTwo.book(10, 20); // return True, The event can be booked.
23 * myCalendarTwo.book(50, 60); // return True, The event can be booked.
24 * myCalendarTwo.book(10, 40); // return True, The event can be double booked.
25 * myCalendarTwo.book(5, 15); // return False, The event cannot be booked, because it would result in a triple booking.
26 * myCalendarTwo.book(5, 10); // return True, The event can be booked, as it does not use time 10 which is already double booked.
27 * myCalendarTwo.book(25, 55); // return True, The event can be booked, as the time in [25, 40) will be double booked with the third event, the time [40, 50) will be single booked, and the time [50, 55) will be double booked with the second event.
28 *
29 *
30 * Constraints:
31 *
32 * 0 <= start < end <= 10^9
33 * At most 1000 calls will be made to book.
34 *
35 */
36pub struct Solution {}
37
38// problem: https://leetcode.com/problems/my-calendar-ii/
39// discuss: https://leetcode.com/problems/my-calendar-ii/discuss/?currentPage=1&orderBy=most_votes&query=
40
41// submission codes start here
42
43struct MyCalendarTwo {
44 vec![]
45 }
46
47
48/**
49 * `&self` means the method takes an immutable reference.
50 * If you need a mutable reference, change it to `&mut self` instead.
51 */
52impl MyCalendarTwo {
53
54 fn new() -> Self {
55
56 }
57
58 fn book(&self, start: i32, end: i32) -> bool {
59
60 }
61}
62
63/**
64 * Your MyCalendarTwo object will be instantiated and called as such:
65 * let obj = MyCalendarTwo::new();
66 * let ret_1: bool = obj.book(start, end);
67 */
68
69// submission codes end
70
71#[cfg(test)]
72mod tests {
73 use super::*;
74
75 #[test]
76 fn test_731() {
77 }
78}
79
Back
© 2025 bowen.ge All Rights Reserved.