93. Meeting rooms needed
Each meeting runs from its start time to its end time, written [start, end]. A room holds one meeting at a time,
and a meeting that ends at 10 frees its room for a meeting that starts at 10.
Write a function rooms_needed(meetings) that takes the day's meetings, in any order, and returns the fewest rooms
that can hold them all. That is the largest number of meetings going on at the same moment.
- Meetings that touch, one ending as the next starts, don't overlap:
[1, 5]and[5, 8]fit in one room. - No meetings need no rooms: 0.
Example 1
meetings = [[9, 10], [9, 12], [11, 13]]2Example 2
meetings = [[1, 5], [5, 8]]1Example 3
meetings = []0Constraints
meetingshas 0 to 200 meetings, each[start, end]with whole numbers from 0 to 10,000 andstart < end.
Submit also runs 8 more tests you can't see.
- The number of rooms in use goes up by one at each start and down by one at each end. The answer is the highest it ever gets.
- Sort the start times and the end times as two separate lists. Go through the starts in order; before counting a start, take off every meeting whose end comes at or before it (
end <= start). <=is what lets a room be reused by a meeting that starts just as another ends. Keep the largest count seen.
Solve it without hints for 3 stars, with hints for 2, after seeing the solution for 1.
One way to solve it, once you've had a go.
Solving it after seeing the solution earns 1 star.
Your Submits show here, with their code, so you can go back to one. They stay in this browser.
def rooms_needed(meetings):
"""Find the fewest rooms that hold all the meetings.
meetings: list of [start, end] pairs, in any order
Returns: int, the most meetings going on at once
"""
# Write your code here
pass
Run your code to see how it does on the test cases, or Submit to check every test.