CodeArchon

mediumIntervalsFacebookGoogleMicrosoft

Minimum Rooms Required

A conference organizer finds the minimum number of rooms to host all meetings without conflicts.

Given meeting intervals, return the minimum number of conference rooms required.

Choose approach

1/4
Narrationstep 1 of 4

Start 0 vs End 10. rooms=1, max=1.

1def minMeetingRooms(intervals):
2 starts = sorted(i[0] for i in intervals)
3 ends = sorted(i[1] for i in intervals)
4 rooms = maxRooms = j = 0
5 for s in starts:
6 if s < ends[j]: rooms += 1
7 else: j += 1
8 maxRooms = max(maxRooms, rooms)
9 return maxRooms
Variables
i=0
startAt=0
rooms=1
max=1

How to think about it

  1. Peak concurrent intervals = answer.
  2. Equivalent to a chronological event sweep.

Edge cases to catch in interview

  • Empty -> 0.
  • All overlap -> n rooms.

Examples

Example 11
{"intervals":[[7,10],[2,4]]}

Try your own input

SpacePlay / pause
← / →Previous / next step
RReset to start
. or >Cycle speed