[Leetcode] 0253. Meeting Rooms II

Explore diverse LeetCode solutions in Python, C++, JavaScript, SQL, and TypeScript. Ideal for interview prep, learning, and code practice in multiple programming languages. Github Repo Link

內容目錄

Medium


Given an array of meeting time intervals intervals where intervals[i] = [starti, endi], return the minimum number of conference rooms required.

Example 1:

Input: intervals = [[0,30],[5,10],[15,20]]
Output: 2

Example 2:

Input: intervals = [[7,10],[2,4]]
Output: 1

Constraints:

  • 1 <= intervals.length <= 104
  • 0 <= starti < endi <= 106

Python

				
					# time complexity: O(nlogn)
# space complexity: O(n)
import heapq
from typing import List


class Solution:
    def minMeetingRooms(self, intervals: List[List[int]]) -> int:
        if not intervals:
            return 0
        freeRooms = []
        intervals.sort()
        heapq.heappush(freeRooms, intervals[0][1])
        for interval in intervals[1:]:
            if freeRooms[0] <= interval[0]:
                heapq.heappop(freeRooms)
            heapq.heappush(freeRooms, interval[1])

        return len(freeRooms)


intervals = [[2, 8], [3, 4], [3, 9], [5, 11], [8, 20], [11, 15]]
print(Solution().minMeetingRooms(intervals))
				
			
zh_TW繁體中文