Skip to content

Commit 914f264

Browse files
committed
Add week 13 solutions: meetingRoomsII
1 parent 5ede98e commit 914f264

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

meeting-rooms-ii/yolophg.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Time Complexity: O(n log n)
2+
// Space Complexity: O(n)
3+
4+
export class Solution {
5+
minMeetingRooms(intervals) {
6+
// separate start and end times into different arrays
7+
let startTimes = intervals.map((interval) => interval[0]);
8+
let endTimes = intervals.map((interval) => interval[1]);
9+
10+
// sort the start and end times
11+
startTimes.sort((a, b) => a - b);
12+
endTimes.sort((a, b) => a - b);
13+
14+
let startPointer = 0;
15+
let endPointer = 0;
16+
let rooms = 0;
17+
let maxRooms = 0;
18+
19+
// iterate over all the start times
20+
while (startPointer < intervals.length) {
21+
// if the start time is less than the end time, a new room is needed
22+
if (startTimes[startPointer] < endTimes[endPointer]) {
23+
rooms++;
24+
startPointer++;
25+
} else {
26+
// if the start time is not less than the end time, free up a room
27+
rooms--;
28+
endPointer++;
29+
}
30+
31+
// update the maximum number of rooms needed
32+
maxRooms = Math.max(maxRooms, rooms);
33+
}
34+
35+
// return the maximum number of rooms needed
36+
return maxRooms;
37+
}
38+
}

0 commit comments

Comments
 (0)