Skip to main content
Back to problems
#731
Medium Algorithms

My calendar ii

Array Binary Search Design Segment Tree Prefix Sum Ordered Set
62.9% acceptance
Feb 21, 2026
2270
188
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. A triple booking happens when three events have some non-empty intersection (i.e., some moment is common to all the three events.). The event can be represented as a pair of integers startTime and endTime that represents a booking on the half-open interval [startTime, endTime), the range of real numbers x such that startTime <= x < endTime. Implement the MyCalendarTwo class: MyCalendarTwo() Initializes the calendar object. boolean book(int startTime, int endTime) 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.

Solution

Rust
Time O(2^n)
Space O(n)
LeetCode
solution.rs
/*
 * 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.
 * A triple booking happens when three events have some non-empty intersection (i.e., some moment is common to all the three events.).
 * The event can be represented as a pair of integers startTime and endTime that represents a booking on the half-open interval [startTime, endTime), the range of real numbers x such that startTime <= x < endTime.
 * Implement the MyCalendarTwo class:
 * MyCalendarTwo() Initializes the calendar object.
 * boolean book(int startTime, int endTime) 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.
 * Example 1:
 * Input
 * ["MyCalendarTwo", "book", "book", "book", "book", "book", "book"]
 * [[], [10, 20], [50, 60], [10, 40], [5, 15], [5, 10], [25, 55]]
 * Output
 * [null, true, true, true, false, true, true]
 * Explanation
 * MyCalendarTwo myCalendarTwo = new MyCalendarTwo();
 * myCalendarTwo.book(10, 20); // return True, The event can be booked.
 * myCalendarTwo.book(50, 60); // return True, The event can be booked.
 * myCalendarTwo.book(10, 40); // return True, The event can be double booked.
 * myCalendarTwo.book(5, 15);  // return False, The event cannot be booked, because it would result in a triple booking.
 * myCalendarTwo.book(5, 10); // return True, The event can be booked, as it does not use time 10 which is already double booked.
 * 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.
 * Constraints:
 * 0 <= start < end <= 109
 * At most 1000 calls will be made to book.

 * struct MyCalendarTwo {

 * }


 * /** 
 *  * `&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 {

 *     }

 *     fn book(&self, start_time: i32, end_time: i32) -> bool {

 *     }
 * }
 */
struct MyCalendarTwo {
  single: Vec<(i32, i32)>,
  double: Vec<(i32, i32)>,
}

impl MyCalendarTwo {
  fn new() -> Self {
    MyCalendarTwo { single: vec![], double: vec![] }
  }

  fn book(&mut self, start_time: i32, end_time: i32) -> bool {
    // Check triple booking against double
    for &(s, e) in &self.double {
      if start_time < e && end_time > s {
        return false;
      }
    }
    // Add overlaps with single to double
    for &(s, e) in &self.single {
      let os = start_time.max(s);
      let oe = end_time.min(e);
      if os < oe {
        self.double.push((os, oe));
      }
    }
    self.single.push((start_time, end_time));
    true
  }
}