Given one meeting room and N meetings represented by two arrays, start and end, where start[i] represents the start time of the ith meeting and end[i] represents the end time of the ith meeting, determine the maximum number of meetings that can be accommodated in the meeting room if only one meeting can be held at a time.
Input : Start = [1, 3, 0, 5, 8, 5] , End = [2, 4, 6, 7, 9, 9]
Output : 4
Explanation : The meetings that can be accommodated in meeting room are (1,2) , (3,4) , (5,7) , (8,9).
Input : Start = [10, 12, 20] , End = [20, 25, 30]
Output : 1
Explanation : Given the start and end time, only one meeting can be held in meeting room.
Input : Start = [1, 4, 6, 9] , End = [2, 5, 7, 12]
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
// Comparator function to sort meetings based on end times
static bool comparator(const pair<int, int>& a, const pair<int, int>& b) {
// Sort by end time in ascending order
return a.second < b.second;
}
// Function to find the maximum number of meetings that can be held
int maxMeetings(vector<int>& start, vector<int>& end) {
int n = start.size();
// Vector to store meetings
vector<pair<int, int>> meetings;
// Fill the meetings vector with start and end times
for (int i = 0; i < n; i++) {
meetings.push_back({start[i], end[i]});
}
// Sort the meetings based on the custom comparator
sort(meetings.begin(), meetings.end(), comparator);
// The end time of last selected meeting
int limit = meetings[0].second;
// Initialize count
int count = 1;
/*Iterate through the meetings
to select the maximum number
of non-overlapping meetings*/
for (int i = 1; i < n; i++) {
/*If the current meeting starts
after the last selected meeting ends*/
if (meetings[i].first > limit) {
/*Update the limit to the end
time of the current meeting*/
limit = meetings[i].second;
// Increment count
count++;
}
}
// Return count
return count;
}
};
int main() {
Solution obj;
// Start and end times of the meetings
vector<int> start = {1, 3, 0, 5, 8, 5};
vector<int> end = {2, 4, 6, 7, 9, 9};
// Get the maximum number of meetings that can be held
int maxMeetings = obj.maxMeetings(start, end);
// Output the maximum number of meetings
cout << "Maximum number of meetings: " << maxMeetings << endl;
return 0;
}
import java.util.*;
class Solution {
// Comparator function to sort meetings based on end times
static class MeetingComparator implements Comparator<int[]> {
public int compare(int[] a, int[] b) {
// Sort by end time in ascending order
return Integer.compare(a[1], b[1]);
}
}
// Function to find the maximum number of meetings that can be held
public int maxMeetings(int[] start, int[] end) {
int n = start.length;
// List to store meetings
List<int[]> meetings = new ArrayList<>();
// Fill the meetings list with start and end times
for (int i = 0; i < n; i++) {
meetings.add(new int[]{start[i], end[i]});
}
// Sort the meetings based on the custom comparator
Collections.sort(meetings, new MeetingComparator());
// The end time of last selected meeting
int limit = meetings.get(0)[1];
// Initialize count
int count = 1;
/*Iterate through the meetings
to select the maximum number
of non-overlapping meetings*/
for (int i = 1; i < n; i++) {
/*If the current meeting starts
after the last selected meeting ends*/
if (meetings.get(i)[0] > limit) {
/*Update the limit to the end
time of the current meeting*/
limit = meetings.get(i)[1];
// Increment count
count++;
}
}
// Return count
return count;
}
public static void main(String[] args) {
Solution obj = new Solution();
// Start and end times of the meetings
int[] start = {1, 3, 0, 5, 8, 5};
int[] end = {2, 4, 6, 7, 9, 9};
// Get the maximum number of meetings that can be held
int maxMeetings = obj.maxMeetings(start, end);
// Output the maximum number of meetings
System.out.println("Maximum number of meetings: " + maxMeetings);
}
}
class Solution:
# Function to find the maximum number of meetings that can be held
def maxMeetings(self, start, end):
n = len(start)
# List to store meetings
meetings = []
# Fill the meetings list with start and end times
for i in range(n):
meetings.append((start[i], end[i]))
# Sort the meetings based on end times in ascending order
meetings.sort(key=lambda x: x[1])
# The end time of last selected meeting
limit = meetings[0][1]
# Initialize count
count = 1
# Iterate through the meetings to select the maximum number of non-overlapping meetings
for i in range(1, n):
# If the current meeting starts after the last selected meeting ends
if meetings[i][0] > limit:
# Update the limit to the end time of the current meeting
limit = meetings[i][1]
# Increment count
count += 1
# Return count
return count
# Example usage
if __name__ == "__main__":
obj = Solution()
# Start and end times of the meetings
start = [1, 3, 0, 5, 8, 5]
end = [2, 4, 6, 7, 9, 9]
# Get the maximum number of meetings that can be held
maxMeetings = obj.maxMeetings(start, end)
# Output the maximum number of meetings
print("Maximum number of meetings:", maxMeetings)
class Solution {
// Comparator function to sort meetings based on end times
static comparator(a, b) {
// Sort by end time in ascending order
return a[1] - b[1];
}
// Function to find the maximum number of meetings that can be held
maxMeetings(start, end) {
const n = start.length;
// Array to store meetings
const meetings = [];
// Fill the meetings array with start and end times
for (let i = 0; i < n; i++) {
meetings.push([start[i], end[i]]);
}
// Sort the meetings based on the custom comparator
meetings.sort(Solution.comparator);
// The end time of last selected meeting
let limit = meetings[0][1];
// Initialize count
let count = 1;
/*Iterate through the meetings
to select the maximum number
of non-overlapping meetings*/
for (let i = 1; i < n; i++) {
/*If the current meeting starts
after the last selected meeting ends*/
if (meetings[i][0] > limit) {
/*Update the limit to the end
time of the current meeting*/
limit = meetings[i][1];
// Increment count
count++;
}
}
// Return count
return count;
}
}
// Example usage
const obj = new Solution();
// Start and end times of the meetings
const start = [1, 3, 0, 5, 8, 5];
const end = [2, 4, 6, 7, 9, 9];
// Get the maximum number of meetings that can be held
const maxMeetings = obj.maxMeetings(start, end);
// Output the maximum number of meetings
console.log("Maximum number of meetings:", maxMeetings);
Q: Why sort by end time?
A: Sorting by end time ensures that meetings that finish earlier are prioritized. This allows more meetings to be accommodated since the room is freed up sooner.
Q: What if all meetings overlap?
A: If all meetings overlap, only one meeting (the one with the earliest end time) can be scheduled.
Q: What if there are multiple meeting rooms?
A: If multiple rooms are available, the problem becomes a meeting room allocation problem, which can be solved using a min-heap to track the end times of ongoing meetings.
Q: What is the difference between this and the interval scheduling maximization problem?
A: This is a specific case of interval scheduling maximization, where the goal is to select the maximum number of non-overlapping intervals from a set of intervals.