forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0253-meeting-rooms-ii.js
39 lines (31 loc) · 937 Bytes
/
0253-meeting-rooms-ii.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/**
* https://leetcode.com/problems/meeting-rooms-ii/
* Time O((N * logN) + (M * logM)) | Space O(1)
* @param {number[][]} intervals
* @return {number}
*/
var minMeetingRooms = function (intervals) {
const { start, end } = splitIntervals(intervals);
let [minRooms, startIndex, endIndex] = [0, 0, 0];
while (startIndex < intervals.length) {
const [currStart, prevEnd] = [start[startIndex], end[endIndex]];
const hasGap = prevEnd <= currStart;
if (hasGap) {
minRooms--;
endIndex++;
}
minRooms++;
startIndex++;
}
return minRooms;
};
const splitIntervals = (intervals, start = [], end = []) => {
for (const [startTime, endTime] of intervals) {
start.push(startTime);
end.push(endTime);
}
const comparator = (a, b) => a - b;
start.sort(comparator);
end.sort(comparator);
return { start, end };
};