Class MyCalendar

java.lang.Object
g0701_0800.s0729_my_calendar_i.MyCalendar

public class MyCalendar extends Object
729 - My Calendar I.<p>Medium</p> <p>You are implementing a program to use as your calendar. We can add a new event if adding the event will not cause a <strong>double booking</strong>.</p> <p>A <strong>double booking</strong> happens when two events have some non-empty intersection (i.e., some moment is common to both events.).</p> <p>The event can be represented as a pair of integers <code>start</code> and <code>end</code> that represents a booking on the half-open interval <code>[start, end)</code>, the range of real numbers <code>x</code> such that <code>start <= x < end</code>.</p> <p>Implement the <code>MyCalendar</code> class:</p> <ul> <li><code>MyCalendar()</code> Initializes the calendar object.</li> <li><code>boolean book(int start, int end)</code> Returns <code>true</code> if the event can be added to the calendar successfully without causing a <strong>double booking</strong>. Otherwise, return <code>false</code> and do not add the event to the calendar.</li> </ul> <p><strong>Example 1:</strong></p> <p><strong>Input</strong></p> <p>[&ldquo;MyCalendar&rdquo;, &ldquo;book&rdquo;, &ldquo;book&rdquo;, &ldquo;book&rdquo;]</p> <p>[ [], [10, 20], [15, 25], [20, 30]]</p> <p><strong>Output:</strong> [null, true, false, true]</p> <p><strong>Explanation:</strong></p> <pre><code> MyCalendar myCalendar = new MyCalendar(); myCalendar.book(10, 20); // return True myCalendar.book(15, 25); // return False, It can not be booked because time 15 is already booked by another event. 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. </code></pre> <p><strong>Constraints:</strong></p> <ul> <li><code>0 <= start < end <= 10<sup>9</sup></code></li> <li>At most <code>1000</code> calls will be made to <code>book</code>.</li> </ul>
  • Constructor Details

    • MyCalendar

      public MyCalendar()
  • Method Details

    • book

      public boolean book(int start, int end)