Given a two dimensional array arr[][] of dimensions N * 2 which contains the starting and ending time for N meetings on a given day. The task is to print a list of time slots during which most number of concurrent meetings can be held.
Examples:
Input: arr[][] = {{100, 300}, {145, 215}, {200, 230}, {215, 300}, {215, 400}, {500, 600}, {600, 700}}
Output: [215, 230]
Explanation:
The given 5 meetings overlap at {215, 230}.Input: arr[][] = {{100, 200}, {50, 300}, {300, 400}}
Output: [100, 200]
Approach: The idea is to use a Min-Heap to solve this problem. Below are the steps:
- Sort the array based on the start time of meetings.
- Initialize a min-heap.
- Initialize variables max_len, max_start and max_end to store maximum size of min heap, start time and end time of concurrent meetings respectively.
- Iterate over the sorted array and keep popping from min_heap until arr[i][0] becomes smaller than the elements of the min_heap, i.e. pop all the meetings having ending time smaller than the starting time of current meeting, and push arr[i][1] in to min_heap.
- If the size of min_heap exceeds max_len, then update max_len = size(min_heap), max_start = meetings[i][0] and max_end = min_heap_element.
- Return the value of max_start and max_end at the end.
Below is the implementation of the above approach:
C++14
// C++14 implementation of the // above approach #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 of // maximum concurrent meeting void maxConcurrentMeetingSlot( vector<vector< int >> meetings) { // Sort array by // start time of meeting sort(meetings.begin(), meetings.end(), cmp); // Declare Minheap priority_queue< int , vector< int >, greater< int >> pq; // Insert first meeting end time pq.push(meetings[0][1]); // Initialize max_len, // max_start and max_end int max_len = 0, max_start = 0; int max_end = 0; // Traverse over sorted array // to find required slot for ( auto k : meetings) { // Pop all meetings that end // before current meeting while (pq.size() > 0 && k[0] >= pq.top()) pq.pop(); // Push current meeting end time pq.push(k[1]); // Update max_len, max_start // and max_end if size of // queue is greater than max_len if (pq.size() > max_len) { max_len = pq.size(); max_start = k[0]; max_end = pq.top(); } } // Print slot of maximum // concurrent meeting cout << max_start << " " << max_end; } // Driver Code int main() { // Given array of meetings vector<vector< int >> meetings = { { 100, 200 }, { 50, 300 }, { 300, 400 } }; // Function call maxConcurrentMeetingSlot(meetings); } // This code is contributed by mohit kumar 29 |
Java
// Java implementation of the // above approach import java.util.*; import java.lang.*; class GFG { // Function to find time slot of // maximum concurrent meeting static void maxConcurrentMeetingSlot( int [][] meetings) { // Sort array by // start time of meeting Arrays.sort(meetings, (a, b) -> (a[ 0 ] != b[ 0 ]) ? a[ 0 ] - b[ 0 ] : a[ 1 ] - b[ 1 ]); // Declare Minheap PriorityQueue<Integer> pq = new PriorityQueue<>(); // Insert first meeting end time pq.add(meetings[ 0 ][ 1 ]); // Initialize max_len, // max_start and max_end int max_len = 0 , max_start = 0 ; int max_end = 0 ; // Traverse over sorted array // to find required slot for ( int [] k : meetings) { // Pop all meetings that end // before current meeting while (!pq.isEmpty() && k[ 0 ] >= pq.peek()) pq.poll(); // Push current meeting end time pq.add(k[ 1 ]); // Update max_len, max_start // and max_end if size of // queue is greater than max_len if (pq.size() > max_len) { max_len = pq.size(); max_start = k[ 0 ]; max_end = pq.peek(); } } // Print slot of maximum // concurrent meeting System.out.println( max_start + " " + max_end); } // Driver Code public static void main(String[] args) { // Given array of meetings int meetings[][] = { { 100 , 200 }, { 50 , 300 }, { 300 , 400 } }; // Function Call maxConcurrentMeetingSlot(meetings); } } |
100 200
Time Complexity: O(N * logN)
Auxiliary Space: O(N)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.