mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-01-11 02:58:13 +08:00
37 lines
1.8 KiB
HTML
37 lines
1.8 KiB
HTML
<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> </p>
|
|
<p><strong>Example 1:</strong></p>
|
|
|
|
<pre>
|
|
<strong>Input</strong>
|
|
["MyCalendar", "book", "book", "book"]
|
|
[[], [10, 20], [15, 25], [20, 30]]
|
|
<strong>Output</strong>
|
|
[null, true, false, true]
|
|
|
|
<strong>Explanation</strong>
|
|
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.</pre>
|
|
|
|
<p> </p>
|
|
<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>
|