All problems

Problem 108

Minimum Meeting Rooms

Medium · 20 pts · Array, Sorting, Heap

Problem

You are given N meetings, each as a time interval [start, end) that includes its start but not its end. Find the minimum number of rooms needed so that meetings that overlap in time are never in the same room. A meeting that ends at time t frees its room for a meeting that starts at time t. The answer is the largest number of meetings running at the same moment. Sort all start times and all end times separately and sweep through them with two pointers, or use a min-heap of end times for the rooms in use.

Input

First line: integer N. Next N lines: start and end of a meeting.

Output

Print the minimum number of rooms.

Constraints

1 <= N <= 100000; 0 <= start < end <= 1000000000

Example input

3
0 30
5 10
15 20

Example output

2

Explanation

[0, 30) overlaps [5, 10), so two rooms are needed at time 5. When [15, 20) starts, [5, 10) has already ended and its room is free. At most two meetings ever run at once, so the answer is 2.

Every problem here can be used in a RelayTrixCode team match. Create a room.

Previous: Meeting Rooms Next: Kth Smallest in a Sorted Matrix