|
2 | 2 |
|
3 | 3 |
|
4 | 4 | # Solution 01: Official solution from leetcode itself
|
| 5 | +# Definition for an interval. |
| 6 | +class Interval(object): |
| 7 | + def __init__(self, s=0, e=0): |
| 8 | + self.start = s |
| 9 | + self.end = e |
| 10 | + |
| 11 | + |
| 12 | +# Official solution from leetcode itself |
5 | 13 | class Solution(object):
|
6 | 14 | def minMeetingRooms(self, intervals):
|
7 | 15 | """
|
8 |
| - :type intervals: List[List[int]] |
| 16 | + :type intervals: List[Interval] |
9 | 17 | :rtype: int
|
10 | 18 | """
|
11 |
| - if len(intervals) < 1: |
| 19 | + |
| 20 | + # If there is no meeting to schedule then no room needs to be allocated. |
| 21 | + if not intervals: |
12 | 22 | return 0
|
| 23 | + |
13 | 24 | # The heap initialization
|
14 |
| - usedRooms = [] |
| 25 | + occupied_rooms = [] |
15 | 26 |
|
16 |
| - # Sort the meetings in increasing order of their start time |
| 27 | + # Sort the meetings in increasing order of their start time. |
17 | 28 | intervals.sort(key= lambda x: x[0])
|
18 | 29 |
|
19 |
| - # Add the first meetinng. we have to give a new room to the first meeting |
20 |
| - heapq.heappush(usedRooms, intervals[0][1]) |
| 30 | + # Add the first meeting. We have to give a new room to the first meeting. |
| 31 | + # We are storing the meeting's END time into the heap, So that we can know which meetiing is going to end first and we can assign new meetings aaccordingly |
| 32 | + heapq.heappush(occupied_rooms, intervals[0][1]) |
21 | 33 |
|
22 | 34 | # For all the remaining meeting rooms
|
23 | 35 | for interval in intervals[1:]:
|
24 | 36 |
|
25 | 37 | # If the room due to free up the earliest is free, assign that room to this meeting.
|
26 |
| - if interval[0] >= usedRooms[0]: |
27 |
| - heapq.heappop(usedRooms) |
| 38 | + if occupied_rooms[0] <= interval[0]: |
| 39 | + heapq.heappop(occupied_rooms) |
28 | 40 |
|
29 | 41 | # If a new room is to be assigned, then also we add to the heap,
|
30 | 42 | # If an old room is allocated, then also we have to add to the heap with updated end time.
|
31 |
| - heapq.heappush(usedRooms, interval[1]) |
| 43 | + heapq.heappush(occupied_rooms, interval[1]) |
| 44 | + |
| 45 | + return len(occupied_rooms) |
32 | 46 |
|
33 |
| - # If a new room is to be assigned, then also we add to the heap, |
34 |
| - # If an old room is allocated, then also we have to add to the heap with updated end time. |
35 |
| - return len(usedRooms) |
36 | 47 |
|
37 | 48 |
|
38 | 49 |
|
|
0 commit comments