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

📅  最后修改于: 2021-10-28 01:51:07             🧑  作者: Mango

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

例子:

方法:想法是使用Min-Heap来解决这个问题。以下是步骤:

  1. 根据会议的开始时间对数组进行排序。
  2. 初始化一个min-heap
  3. 初始化变量max_lenmax_startmax_end分别存储最小堆的最大大小、并发会议的开始时间和结束时间。
  4. 迭代排序后的数组并继续从min_heap弹出直到arr[i][0]变得小于min_heap的元素,即弹出所有结束时间小于当前会议开始时间的会议,并推送arr[i] [1]min_heap
  5. 如果 min_heap 的大小超过max_len ,则更新max_len = size(min_heap)max_start = meeting[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);
    }
}


Javascript


输出:
100 200

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

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程