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

You need to determine the maximum number of meetings that are happening at the same time, which equals the minimum number of rooms needed.

For example, if meetings are [[0,30],[5,10],[15,20]], we need 2 rooms because meetings [0,30] and [5,10] overlap.

Input & Output

Example 1 — Basic Overlapping
$ Input: intervals = [[0,30],[5,10],[15,20]]
Output: 2
💡 Note: Meeting [0,30] overlaps with both [5,10] and [15,20], but [5,10] and [15,20] don't overlap with each other. Maximum concurrent meetings is 2 (at times 5-10 and 15-20).
Example 2 — No Overlap
$ Input: intervals = [[7,10],[2,4]]
Output: 1
💡 Note: The meetings don't overlap: [2,4] ends before [7,10] starts. Only 1 room needed since they can use the same room sequentially.
Example 3 — Adjacent Meetings
$ Input: intervals = [[1,5],[8,9],[8,9]]
Output: 2
💡 Note: First meeting [1,5] doesn't overlap with others. The two [8,9] meetings start at exactly the same time, so we need 2 rooms for them.

Constraints

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

Visualization

Tap to expand
Meeting Rooms II - Optimal Solution INPUT Meeting Time Intervals 0 10 20 30 [0, 30] [5,10] [15,20] intervals = [[0,30],[5,10],[15,20]] Overlapping meetings shown ALGORITHM STEPS 1 Extract Events Start: +1, End: -1 Time: 0 5 10 15 20 30 +1 +1 -1 +1 -1 -1 S=start, E=end events 2 Sort by Time Process ends before starts 3 Sweep Line Count active meetings t=0: cnt=1 | t=5: cnt=2 max=2 (peak!) t=10: cnt=1 | ... 4 Track Maximum Return peak count FINAL RESULT Minimum Rooms Required Room 1 Meeting [0,30] Room 2 [5,10] [15,20] Non-overlapping meetings Output: 2 OK - 2 rooms needed Key Insight: The minimum number of rooms equals the maximum number of overlapping meetings at any point in time. Use a sweep line algorithm: treat each start as +1 and each end as -1, then find the peak count. Time Complexity: O(n log n) | Space Complexity: O(n) TutorialsPoint - Meeting Rooms II | Sweep Line Approach
Asked in
Google 85 Facebook 72 Amazon 68 Microsoft 45 Apple 38
285.0K Views
High Frequency
~25 min Avg. Time
4.2K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen