Overview
Given an array of meeting time intervals where intervals[i] = [start, end], find the minimum number of conference rooms required so that no two overlapping meetings share the same room.
Implement minMeetingRooms(intervals).
Constraints
- Each interval is a two-element array
[start, end]wherestart < end. - Input may be unsorted.
- Return an integer representing the minimum rooms needed.
- Adjacent meetings (
[0,5],[5,10]) do NOT conflict — the first ends as the second begins. - Aim for $O(n \log n)$ time.
Examples
minMeetingRooms([[0,30],[5,10],[15,20]]); // → 2 — [5,10] overlaps with [0,30], but [15,20] also overlaps with [0,30] // at most 2 run simultaneously minMeetingRooms([[7,10],[2,4]]); // → 1 — no overlap minMeetingRooms([[0,5],[5,10],[0,10]]); // → 2 — [0,5] and [0,10] overlap; [5,10] and [0,10] overlap
Notes
- Sweep-line approach: split each interval into two events — a start (+1) and an end (−1). Sort the events by time (break ties: ends before starts so a room freed at time T can be reused at time T). Sweep and track the running count; the maximum is the answer.
- Alternatively, sort starts and ends in separate arrays and use two pointers.
- Edge cases: empty array → 0, single meeting → 1.
Solution
Reveal solution
function minMeetingRooms(intervals) {
if (intervals.length === 0) return 0;
const events = [];
for (const [start, end] of intervals) {
events.push([start, 1]); // meeting starts
events.push([end, -1]); // meeting ends
}
// Sort by time; if same time, ends (-1) before starts (+1)
events.sort((a, b) => a[0] - b[0] || a[1] - b[1]);
let rooms = 0;
let maxRooms = 0;
for (const [, delta] of events) {
rooms += delta;
maxRooms = Math.max(maxRooms, rooms);
}
return maxRooms;
}Resources
minimum-meeting-rooms.js
Minimum Meeting Rooms Needed
hardcodingAlgorithmsSorting
Overview
Given an array of meeting time intervals where intervals[i] = [start, end], find the minimum number of conference rooms required so that no two overlapping meetings share the same room.
Implement minMeetingRooms(intervals).
Constraints
- Each interval is a two-element array
[start, end]wherestart < end. - Input may be unsorted.
- Return an integer representing the minimum rooms needed.
- Adjacent meetings (
[0,5],[5,10]) do NOT conflict — the first ends as the second begins. - Aim for $O(n \log n)$ time.
Examples
minMeetingRooms([[0,30],[5,10],[15,20]]); // → 2 — [5,10] overlaps with [0,30], but [15,20] also overlaps with [0,30] // at most 2 run simultaneously minMeetingRooms([[7,10],[2,4]]); // → 1 — no overlap minMeetingRooms([[0,5],[5,10],[0,10]]); // → 2 — [0,5] and [0,10] overlap; [5,10] and [0,10] overlap
Notes
- Sweep-line approach: split each interval into two events — a start (+1) and an end (−1). Sort the events by time (break ties: ends before starts so a room freed at time T can be reused at time T). Sweep and track the running count; the maximum is the answer.
- Alternatively, sort starts and ends in separate arrays and use two pointers.
- Edge cases: empty array → 0, single meeting → 1.
Solution
Reveal solution
function minMeetingRooms(intervals) {
if (intervals.length === 0) return 0;
const events = [];
for (const [start, end] of intervals) {
events.push([start, 1]); // meeting starts
events.push([end, -1]); // meeting ends
}
// Sort by time; if same time, ends (-1) before starts (+1)
events.sort((a, b) => a[0] - b[0] || a[1] - b[1]);
let rooms = 0;
let maxRooms = 0;
for (const [, delta] of events) {
rooms += delta;
maxRooms = Math.max(maxRooms, rooms);
}
return maxRooms;
}Resources
NameTopicDifficulty
103 of 103 problems