Leetcode # 253 (Python): Meeting Rooms II

정욱·2021년 4월 21일
0

Leetcode

목록 보기
18/38

Meeting Rooms II

  • Difficulty: Medium
  • Type: Heap/Priority Que
  • link

Problem

Solution

  • Data structure: min heap
  • Algorithm: priority que
  • Sort the array first in ascending start time
  • Save the end time in the min heap
    • Rational: whenever the new meeting start time is greater than the minimum end time of the past, a room can be reused to host the new meeting
  • Compare meeting start times with the minimum value in the min heap
  • Pop from min heap when start time is greater than the minimum end time in the heap
import heapq
class Solution:
    def minMeetingRooms(self, intervals: List[List[int]]) -> int:
        # Sort the array in ascending start time
        intervals.sort(key=lambda x: (x[0],x[1])) # nlogn
        rooms = []
        for meeting in intervals: # n
            if not rooms:
                heapq.heappush(rooms,meeting[1]) # log n
            # Compare minimum end time with new meeting's start time
            elif rooms[0] <= meeting[0]:
                heapq.heappop(rooms) # log n
                heapq.heappush(rooms,meeting[1]) # log n
            else:
                heapq.heappush(rooms,meeting[1]) # log n

        return len(rooms)

1개의 댓글

comment-user-thumbnail
2024년 5월 29일

Even if you're working remotely, it's important to have occasional meetings, and sometimes you need to find a large space for a presentation. With the best room booking software Unspot, finding a place won't take much time. A modern office booking system helps you find the optimal space by price, time, and location.

답글 달기