Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Meeting Rooms 2 problem in JavaScript
We will be given an array of arrays, each subarray consists of exactly two elements indicating the start and end time of a meeting.
The task of our function is to find the maximum number of meetings one person can take avoiding the conflict of time. The function should finally return this number.
For example −
If the input array describing meeting times is −
const arr = [[5, 40], [10, 20], [25, 35]];
Then the output should be −
const output = 2;
because it's not possible to take all three meetings due to overlapping times but [10, 20] and [25, 35] can be attended.
Problem Analysis
This is a classic "Activity Selection Problem" where we need to select the maximum number of non-overlapping meetings. The optimal approach is to sort meetings by end time and greedily select meetings that don't conflict.
Solution: Greedy Approach
const arr = [[5, 40], [10, 20], [25, 35]];
const maxMeetings = (meetings = []) => {
if (meetings.length === 0) return 0;
// Sort meetings by end time
meetings.sort((a, b) => a[1] - b[1]);
let count = 1; // First meeting is always selected
let lastEndTime = meetings[0][1];
for (let i = 1; i < meetings.length; i++) {
// If current meeting starts after last selected meeting ends
if (meetings[i][0] >= lastEndTime) {
count++;
lastEndTime = meetings[i][1];
}
}
return count;
};
console.log("Meetings:", arr);
console.log("Maximum meetings:", maxMeetings(arr));
// Test with more examples
console.log("Maximum meetings for [[1,3],[2,4],[3,5]]:", maxMeetings([[1,3],[2,4],[3,5]]));
console.log("Maximum meetings for [[1,2],[3,4],[5,6]]:", maxMeetings([[1,2],[3,4],[5,6]]));
Meetings: [ [ 5, 40 ], [ 10, 20 ], [ 25, 35 ] ] Maximum meetings: 2 Maximum meetings for [[1,3],[2,4],[3,5]]: 2 Maximum meetings for [[1,2],[3,4],[5,6]]: 3
How It Works
The algorithm uses a greedy approach:
- Sort by end time: Meetings ending earlier are prioritized
- Select first meeting: Always include the meeting that ends earliest
- Greedy selection: For remaining meetings, select only those that start after the last selected meeting ends
Alternative: Check Meeting Conflicts
Here's a function to check if all meetings can be attended (returns boolean):
const canAttendAll = (meetings = []) => {
// Sort meetings by start time
meetings.sort((a, b) => a[0] - b[0]);
for (let i = 0; i < meetings.length - 1; i++) {
// Check if current meeting ends after next meeting starts
if (meetings[i][1] > meetings[i + 1][0]) {
return false; // Conflict found
}
}
return true; // No conflicts
};
console.log("Can attend all [[5,40],[10,20],[25,35]]:", canAttendAll([[5, 40], [10, 20], [25, 35]]));
console.log("Can attend all [[1,2],[3,4],[5,6]]:", canAttendAll([[1, 2], [3, 4], [5, 6]]));
Can attend all [[5,40],[10,20],[25,35]]: false Can attend all [[1,2],[3,4],[5,6]]: true
Time Complexity
The solution has O(n log n) time complexity due to sorting, where n is the number of meetings. The greedy selection itself is O(n).
Conclusion
The Meeting Rooms problem is solved optimally using a greedy algorithm that sorts meetings by end time and selects non-overlapping meetings. This approach guarantees the maximum number of meetings can be attended.
