Given n meetings in the form of start[] and end[], where start[i] is the start time of ith meeting and end[i] is the end time of ith meeting. The task is to find the maximum number of meetings that can be scheduled in a single room. The meeting room can have only one meeting at a particular time.
Note: The start time of one chosen meeting can't be equal to the end time of any other chosen meeting.
Examples:
Input: start[] = [1, 3, 0, 5, 8, 5], end[] = [2, 4, 6, 7, 9, 9]
Output: 1 2 4 5
Explanation: We can attend the 1st meeting from (1 to 2), then the 2nd meeting from (3 to 4), then the 4th meeting from (5 to 7), and the 5th meeting from (8 to 9).Input: start[] = [10, 12, 20], end[] = [20, 25, 30]
Output: 1
Explanation: We can attend at most one meeting in a single meeting room.
Using Greedy Scheduling - O(n log n) Time and O(n) Space
The idea is to select the maximum number of non-overlapping meetings using a greedy strategy.
- We always pick the meeting that finishes earliest so that we get more room to schedule remaining meetings.
- By sorting meetings based on their finish time, we ensure that at each step we choose the best possible meeting that leaves maximum time for others.
Steps
- Store meetings as
(finish time, index)pairs and sort meetings based on finish time - Pick the first meeting (earliest finish). Set
lastFinish = finish time of first meeting - Traverse remaining meetings: If
start > lastFinish,select meeting and UpdatelastFinish - Store selected meeting indices and sort indices before returning
Below is the implementation of the above approach.
#include <bits/stdc++.h>
using namespace std;
vector<int> maxMeetings(vector<int> &s, vector<int> &f) {
int n = s.size();
// pair<finish_time, index>
vector<pair<int, int>> a(n);
for (int i = 0; i < n; i++) {
a[i] = {f[i], i};
}
// sort by finish time
sort(a.begin(), a.end());
vector<int> ans;
// pick first meeting
int lastFinish = a[0].first;
ans.push_back(a[0].second + 1);
// process remaining meetings
for (int i = 1; i < n; i++) {
if (s[a[i].second] > lastFinish) {
ans.push_back(a[i].second + 1);
lastFinish = a[i].first;
}
}
// sort indices
sort(ans.begin(), ans.end());
return ans;
}
int main() {
vector<int> start = {1, 3, 0, 5, 8, 5};
vector<int> finish = {2, 4, 6, 7, 9, 9};
vector<int> res = maxMeetings(start, finish);
for (int i = 0; i < res.size(); i++) {
cout << res[i] << " ";
}
return 0;
}
import java.util.*;
class GFG {
static List<Integer> maxMeetings(int[] s, int[] f) {
int n = s.length;
// pair<finish_time, index>
int[][] a = new int[n][2];
for (int i = 0; i < n; i++) {
a[i][0] = f[i];
a[i][1] = i;
}
// sort by finish time
Arrays.sort(a, (x, y) -> x[0] - y[0]);
List<Integer> ans = new ArrayList<>();
// pick first meeting
int lastFinish = a[0][0];
ans.add(a[0][1] + 1);
// process remaining meetings
for (int i = 1; i < n; i++) {
if (s[a[i][1]] > lastFinish) {
ans.add(a[i][1] + 1);
lastFinish = a[i][0];
}
}
// sort indices
Collections.sort(ans);
return ans;
}
public static void main(String[] args) {
int[] start = {1, 3, 0, 5, 8, 5};
int[] finish = {2, 4, 6, 7, 9, 9};
List<Integer> res = maxMeetings(start, finish);
for (int x : res)
System.out.print(x + " ");
}
}
def maxMeetings(s, f):
n = len(s)
# pair<finish_time, index>
a = [(f[i], i) for i in range(n)]
# sort by finish time
a.sort()
ans = []
# pick first meeting
lastFinish = a[0][0]
ans.append(a[0][1] + 1)
# process remaining meetings
for i in range(1, n):
if s[a[i][1]] > lastFinish:
ans.append(a[i][1] + 1)
lastFinish = a[i][0]
# sort indices
ans.sort()
return ans
# Driver code
start = [1, 3, 0, 5, 8, 5]
finish = [2, 4, 6, 7, 9, 9]
res = maxMeetings(start, finish)
for x in res:
print(x, end=" ")
using System;
using System.Collections.Generic;
class GFG {
static List<int> MaxMeetings(int[] s, int[] f) {
int n = s.Length;
// pair<finish_time, index>
List<(int, int)> a = new List<(int, int)>();
for (int i = 0; i < n; i++) {
a.Add((f[i], i));
}
// sort by finish time
a.Sort();
List<int> ans = new List<int>();
// pick first meeting
int lastFinish = a[0].Item1;
ans.Add(a[0].Item2 + 1);
// process remaining meetings
for (int i = 1; i < n; i++) {
if (s[a[i].Item2] > lastFinish) {
ans.Add(a[i].Item2 + 1);
lastFinish = a[i].Item1;
}
}
// sort indices
ans.Sort();
return ans;
}
public static void Main() {
int[] start = {1, 3, 0, 5, 8, 5};
int[] finish = {2, 4, 6, 7, 9, 9};
var res = MaxMeetings(start, finish);
foreach (var x in res)
Console.Write(x + " ");
}
}
function maxMeetings(s, f) {
let n = s.length;
// pair<finish_time, index>
let a = [];
for (let i = 0; i < n; i++) {
a.push([f[i], i]);
}
// sort by finish time
a.sort((x, y) => x[0] - y[0]);
let ans = [];
// pick first meeting
let lastFinish = a[0][0];
ans.push(a[0][1] + 1);
// process remaining meetings
for (let i = 1; i < n; i++) {
if (s[a[i][1]] > lastFinish) {
ans.push(a[i][1] + 1);
lastFinish = a[i][0];
}
}
// sort indices
ans.sort((a, b) => a - b);
return ans;
}
// Driver code
let start = [1, 3, 0, 5, 8, 5];
let finish = [2, 4, 6, 7, 9, 9];
let res = maxMeetings(start, finish);
console.log(res.join(" "));
Output
1 2 4 5