Interview Coding Prep: Meeting Rooms with JavaScript Solution
Explanation
We will first sort the list of meeting times based on the start time. To sort based on the sort time, it takes O(nlogn) time complexity. Because
We only have to compare 2 meetings at a time. If the start time of the second meeting is before the end time of the first meeting, then it overlaps. Therefore, we can return false (the person cannot have an overlapping meeting). There is no need for us to compare the first meeting with the third meeting.
The most important part of this question is sorting the meeting times before we compare.
Using the sort() function in Javascript will have a time complexity of O(nlogn). The JavaScript sort method in Google Chrome uses a variation of a merge sort and insertion sort called Timsort, which results in O(nlogn).
Code
Method 1 (incorrect): First iteration to iterate through time with for loop without a sort
class MeetingRooms {
canAttend(intervals){
if (intervals == null || intervals.length == 0){
return true;
}
for (let…