> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# My Calendar III Python Solution with Tests

> Tested Python solution for LeetCode 732 with 18 pytest cases. Generate a practice environment with lcpy.

LeetCode 732, [Hard](/catalog/hard). Topics: [Binary Search](/catalog/topics/binary-search), [Design](/catalog/topics/design), [Segment Tree](/catalog/topics/segment-tree), [Prefix Sum](/catalog/topics/prefix-sum), [Ordered Set](/catalog/topics/ordered-set). [View on LeetCode](https://leetcode.com/problems/my-calendar-iii/description/).

Generate this problem as a practice environment: tested reference solution, 18 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 732   # by problem number
lcpy gen -s my_calendar_iii   # by problem name
```

## Problem

A `k`-booking happens when `k` events have some non-empty intersection (i.e., there is some time that is common to all `k` events.)

You are given some events `[startTime, endTime)`, after each given event, return an integer `k` representing the maximum `k`-booking between all the previous events.

Implement the `MyCalendarThree` class:

* `MyCalendarThree()` Initializes the object.
* `int book(int startTime, int endTime)` Returns an integer `k` representing the largest integer such that there exists a `k`-booking in the calendar.

### Examples

```
Input
["MyCalendarThree", "book", "book", "book", "book", "book", "book"]
[[], [10, 20], [50, 60], [10, 40], [5, 15], [5, 10], [25, 55]]
Output
[null, 1, 1, 2, 3, 3, 3]

Explanation
MyCalendarThree myCalendarThree = new MyCalendarThree();
myCalendarThree.book(10, 20); // return 1
myCalendarThree.book(50, 60); // return 1
myCalendarThree.book(10, 40); // return 2
myCalendarThree.book(5, 15);  // return 3
myCalendarThree.book(5, 10);  // return 3
myCalendarThree.book(25, 55); // return 3
```

### Constraints

* 0 \<= startTime \< endTime \<= 10^9
* At most 400 calls will be made to book.

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/my_calendar_iii/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/my_calendar_iii/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class MyCalendarThree:
    # Boundary delta map: a booking adds +1 at start and -1 at end, so a sweep
    # over the sorted boundaries gives the number of events alive at each point;
    # the answer is a running maximum and only ever grows.
    # Time: O(n log n) per book (n = bookings so far)
    # Space: O(n)
    def __init__(self) -> None:
        self._delta: dict[int, int] = {}
        self._max_k = 0

    def book(self, start_time: int, end_time: int) -> int:
        self._delta[start_time] = self._delta.get(start_time, 0) + 1
        self._delta[end_time] = self._delta.get(end_time, 0) - 1
        active = 0
        for time in sorted(self._delta):
            active += self._delta[time]
            if active > self._max_k:
                self._max_k = active
        return self._max_k
```

## Complexity

| Time | Space |
| - | - |
| O(n log n) per book (n = bookings so far) | O(n) |

## Tags


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.