v9.1.1
Menu

Meeting Rooms II

mediumPROArrays & SortingAmazon DSA

Given an array of meeting time intervals [start, end], find the minimum number of conference rooms required.

A meeting occupies a room from start until end. A meeting that ends at time tt frees its room for a meeting that starts at exactly tt.

min_meeting_rooms(intervals) receives intervals as a list of [start, end] pairs and returns an integer. The example input 0,30;5,10;15,20 is shorthand for [[0, 30], [5, 10], [15, 20]].

Example

Input
0,30;5,10;15,20
Output
2
Reasoning
  • Input: three meetings, each a pair (start, end): (0,30)(0, 30), (5,10)(5, 10) and (15,20)(15, 20).
  • The (0,30)(0, 30) meeting holds one room from t=0t = 0 to t=30t = 30.
  • From t=5t = 5 to t=10t = 10 a second meeting is running alongside it, so 22 rooms are busy. At t=10t = 10 that meeting ends and its room is free again.
  • From t=10t = 10 to t=15t = 15 only one meeting runs. From t=15t = 15 to t=20t = 20 the third meeting needs a second room, and it can take the one freed at t=10t = 10.
  • Output: 2, the largest number of meetings in progress at any single moment. One room would clash at t=5t = 5.
  • Notice the answer is not the meeting count (33): the two short meetings never overlap, so they share a room. A meeting ending at time tt also frees its room for one starting at exactly tt, so (1,5)(1, 5) and (5,8)(5, 8) need just 11 room.

Constraints

  • 1 <= len(intervals) <= 10^4
  • 0 <= start < end <= 10^6

Learn this in

More from Amazon DSA

PRO

Editor locked

The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.

solution.py

Test results

Sign in to run your code against the test cases.