Find the interval which contains maximum number of concurrent meetings | C ++ 14 implementation of the above approach ; Function to find time slot of maximum concurrent meeting ; Sort array by start time of meeting ; Declare Minheap ; Insert first meeting end time ; Initialize max_len , max_start and max_end ; Traverse over sorted array to find required slot ; Pop all meetings that end before current meeting ; Push current meeting end time ; Update max_len , max_start and max_end if size of queue is greater than max_len ; Print slot of maximum concurrent meeting ; Driver Code ; Given array of meetings ; Function call
#include <bits/stdc++.h> NEW_LINE 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 ] ; } void maxConcurrentMeetingSlot ( vector < vector < int > > meetings ) { sort ( meetings . begin ( ) , meetings . end ( ) , cmp ) ; priority_queue < int , vector < int > , greater < int > > pq ; pq . push ( meetings [ 0 ] [ 1 ] ) ; int max_len = 0 , max_start = 0 ; int max_end = 0 ; for ( auto k : meetings ) { while ( pq . size ( ) > 0 && k [ 0 ] >= pq . top ( ) ) pq . pop ( ) ; pq . push ( k [ 1 ] ) ; if ( pq . size ( ) > max_len ) { max_len = pq . size ( ) ; max_start = k [ 0 ] ; max_end = pq . top ( ) ; } } cout << max_start << " ▁ " << max_end ; } int main ( ) { vector < vector < int > > meetings = { { 100 , 200 } , { 50 , 300 } , { 300 , 400 } } ; maxConcurrentMeetingSlot ( meetings ) ; }