Find the interval which contains the maximum number of concurrent meetings


Given a scenario of a company where meetings are held during fixed time slots. These slots might be overlapping or distant. Thus, optimizing meeting efficiency is important in order to accommodate maximum meetings without any conflicts between schedules. In the problem given, we’ll be going through such an optimizing meeting efficiency problem.

Problem Statement

Given a two-dimensional array time[][] containing the start time and end time of all the meetings that are scheduled for that day. The task is to find the time interval when most of the meetings are occurring.

Sample Example 1

Input: time[][] = {{1, 5}, {2, 6}, {3, 7}, {4, 8}}
Output: 4-5

Explanation

All 4 meetings overlap in the time interval 4-5.

Sample Example 2

Input: time[][] = {{1, 4}, {3, 6}, {5, 7}, {2, 9}, {8, 10}}
Output:3-4

Explanation

In the time slot 3-4, 3 meetings are held.

Solution Approach

For solving the above problem, the first step is to sort the time array based on the start time of meetings and a min-heap is used to store the end times of all meetings. Then go through all the meetings in the sorted array, for each meeting −

  • Removing all meetings from min-heap which have ended before the start time of the current meeting.

  • Push the end time of the current meeting.

Pseudocode

function maxSlot(time):
   sort time using the custom comparator cmp
   initialize an empty priority queue named pq
   push the end time of the first meeting in time to the priority queue pq
   initialize len, start, and end as 0
   for each meeting k in time:
      while pq is not empty and k[0] is greater than or equal to pq.top():
         pop elements from pq until k[0] becomes greater than the top element of pq
      push k[1] (end time of the current meeting) into the priority queue pq
      if pq.size() is greater than len:
         Set len as pq.size()
         Set start as k[0] (start time of the current meeting)
         Set end as pq.top() (end time of the meeting at the top of the priority queue)
   print the values of start and end

Example: C++ Implementation

The following code uses min-heap to find the desired time interval.

#include <bits/stdc++.h>
using namespace std;

bool cmp(vector<int> a, vector<int> b){
   if (a[0] != b[0])
      return a[0] < b[0];
   return a[1] - b[1];
}
// Function to find time slot
void maxSlot(vector<vector<int>> time){
   sort(time.begin(), time.end(), cmp);
   priority_queue<int, vector<int>, greater<int>> pq;
   pq.push(time[0][1]);
   int len = 0, start = 0;
   int end = 0;
   // Traverse over the sorted array
   for (auto k : time){
      // Pop all meetings that end before the current meeting
      while (pq.size() > 0 &&
            k[0] >= pq.top())
         pq.pop();
      // Push current meeting end time
      pq.push(k[1]);
      if (pq.size() > len){
         len = pq.size();
         start = k[0];
         end = pq.top();
      }
   }
   cout << start << " " << end;
}
int main(){
   vector<vector<int>> time = {{1, 5}, {2, 6}, {3, 7}, {4, 8}};
   maxSlot(time);
   return 0;
}

Output

4 5

Time Complexity − O(N*log(N))

Space Complexity − O(N)

Conclusion

In conclusion, we have explored the problem of finding the optimal interval with the maximum number of concurrent meetings. The algorithmic approach provided finds the interval with concurrent meetings. To maximize the performance, the time and space complexity of the algorithm is also calculated.

Updated on: 03-Nov-2023

99 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements