📜  查找包含最大并发会议数的时间间隔

📅  最后修改于: 2021-04-27 22:05:43             🧑  作者: Mango

给定一个维度为N * 2的二维数组arr [] [] ,其中包含给定日期N次会议的开始和结束时间。任务是打印一个时隙列表,在此期间可以召开最多数量的并发会议。

例子:

方法:想法是使用最小堆来解决此问题。步骤如下:

  1. 根据会议的开始时间对数组进行排序。
  2. 初始化min-heap
  3. 初始化变量max_lenmax_startmax_end分别存储最小堆的最大大小,并发会议的开始时间和结束时间。
  4. 遍历排序后的数组,并从min_heap弹出,直到arr [i] [0]小于min_heap的元素,即弹出所有结束时间小于当前会议开始时间的会议,并按下arr [i] [1] in min_heap
  5. 如果min_heap的大小超过max_len ,则更新max_len = size(min_heap)max_start = Meetings [i] [0]max_end = min_heap_element。
  6. 最后返回max_startmax_end的值。

下面是上述方法的实现:

C++14
// C++14 implementation of the
// above approach
#include 
using namespace std;
  
bool cmp(vector a,vector 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> meetings)
{
      
    // Sort array by
    // start time of meeting
    sort(meetings.begin(), meetings.end(), cmp);
  
    // Declare Minheap
    priority_queue,
                       greater> 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> 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 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

时间复杂度: O(N * logN)
辅助空间: O(N)