> ## 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.

# Meeting Rooms II Python Solution with Tests

> Tested Python solution for LeetCode 253 with 20 pytest cases. Generate a practice environment with lcpy.

LeetCode 253, Medium. Topics: Array, Two Pointers, Greedy, Sorting, Heap (Priority Queue). [View on LeetCode](https://leetcode.com/problems/meeting-rooms-ii/description/).

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

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

## Problem

Given an array of meeting time intervals consisting of start and end times `[[s1,e1],[s2,e2],...]` (si \< ei), find the minimum number of conference rooms required.

### Examples

```
Input: [[0,30],[5,10],[15,20]]
Output: 2
```

```
Input: [[7,10],[2,4]]
Output: 1
```

### Constraints

* 1 \<= intervals.length \<= 10^4
* 0 \<= starti \< endi \<= 10^6

**Note:** Input types have been changed on April 15, 2019. Please reset to default code definition to get new method signature.

## Solution

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

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n log n)
    # Space: O(n)
    def min_meeting_rooms(self, intervals: list[list[int]]) -> int:
        if not intervals:
            return 0

        # Separate start and end times
        starts = sorted([interval[0] for interval in intervals])
        ends = sorted([interval[1] for interval in intervals])

        rooms = 0
        max_rooms = 0
        start_ptr = end_ptr = 0

        # Two pointer approach
        while start_ptr < len(intervals):
            if starts[start_ptr] < ends[end_ptr]:
                # Meeting starts, need a room
                rooms += 1
                max_rooms = max(max_rooms, rooms)
                start_ptr += 1
            else:
                # Meeting ends, free a room
                rooms -= 1
                end_ptr += 1

        return max_rooms
```

## Complexity

| Time       | Space |
| ---------- | ----- |
| O(n log n) | O(n)  |

## Tags

[Grind](/catalog/grind), [Blind 75](/catalog/blind-75), [NeetCode 150](/catalog/neetcode-150), [NeetCode 250](/catalog/neetcode-250), [NeetCode All](/catalog/neetcode).
