📜  在一个房间内找到最多的会议

📅  最后修改于: 2021-10-26 05:20:53             🧑  作者: Mango

一家公司有一间会议室。有(S[i], F[i])形式的N 个会议,其中S[i]是会议i的开始时间, F[i]是会议i 的结束时间。任务是找出会议室可容纳的最大会议数。打印所有会议号

例子:

方法:
想法是使用贪心方法解决问题,这与活动选择问题相同。

  • 按每对的第二个数字(完成时间)的升序对所有对(会议)进行排序。
  • 选择已排序对的第一个会议作为房间中的第一个会议,并将其推入结果向量,并使用第一个选定会议的第二个值(结束时间)设置变量 time_limet(say)。
  • 从数组的第二对到最后一对迭代,如果当前对的第一个元素(会议开始时间)的值大于先前选择的对完成时间(time_limit),则选择当前对并更新结果向量(将选定的会议号推入向量)和变量 time_limit。
  • 从向量打印会议顺序。

下面是上述方法的实现。

C++
// C++ program to find maximum number of meetings
#include 
using namespace std;
 
// Structure for storing starting time,
// finishing time and position of meeting.
struct meeting {
    int start;
    int end;
    int pos;
};
 
// Comparator function which can compare
// the second element of structure used to
// sort pairs in increasing order of second value.
bool comparator(struct meeting m1, meeting m2)
{
    return (m1.end < m2.end);
}
 
// Function for finding maximum meeting in one room
void maxMeeting(int s[], int f[], int n)
{
    struct meeting meet[n];
    for (int i = 0; i < n; i++)
    {
        // Starting time of meeting i.
        meet[i].start = s[i];
         
        // Finishing time of meeting i
        meet[i].end = f[i];
         
        // Original position/index of meeting
        meet[i].pos = i + 1;
    }
 
    // Sorting of meeting according to their finish time.
    sort(meet, meet + n, comparator);
 
    // Vector for storing selected meeting.
    vector m;
 
    // Initially select first meeting.
    m.push_back(meet[0].pos);
 
    // time_limit to check whether new
    // meeting can be conducted or not.
    int time_limit = meet[0].end;
 
    // Check for all meeting whether it
    // can be selected or not.
    for (int i = 1; i < n; i++) {
        if (meet[i].start >= time_limit)
        {
            // Push selected meeting to vector
            m.push_back(meet[i].pos);
             
            // Update time limit.
            time_limit = meet[i].end;
        }
    }
 
    // Print final selected meetings.
    for (int i = 0; i < m.size(); i++) {
        cout << m[i] << " ";
    }
}
 
// Driver code
int main()
{
    // Starting time
    int s[] = { 1, 3, 0, 5, 8, 5 };
     
    // Finish time
    int f[] = { 2, 4, 6, 7, 9, 9 };
     
    // Number of meetings.
    int n = sizeof(s) / sizeof(s[0]);
 
    // Function call
    maxMeeting(s, f, n);
 
    return 0;
}


Java
// Java program to find maximum number of meetings
import java.util.*;
 
// Comparator function which can compare
// the ending time of the meeting ans
// sort the list
class mycomparator implements Comparator
{
    @Override
    public int compare(meeting o1, meeting o2)
    {
        if (o1.end < o2.end)
        {
             
            // Return -1 if second object is
            // bigger then first
            return -1;
        }
        else if (o1.end > o2.end)
         
            // Return 1 if second object is
            // smaller then first
            return 1;
             
        return 0;
    }
}
 
// Custom class for storing starting time, 
// finishing time and position of meeting.
class meeting
{
    int start;
    int end;
    int pos;
     
    meeting(int start, int end, int pos)
    {
        this.start = start;
        this.end = end;
        this.pos = pos;
    }
}
 
class GFG{
     
// Function for finding maximum meeting in one room
public static void maxMeeting(ArrayList al, int s)
{
     
    // Initialising an arraylist for storing answer
    ArrayList m = new ArrayList<>();
     
    int time_limit = 0;
     
    mycomparator mc = new mycomparator();
     
    // Sorting of meeting according to
    // their finish time.
    Collections.sort(al, mc);
     
    // Initially select first meeting.
    m.add(al.get(0).pos);
     
    // time_limit to check whether new 
    // meeting can be conducted or not.
    time_limit = al.get(0).end;
     
    // Check for all meeting whether it 
    // can be selected or not.
    for(int i = 1; i < al.size(); i++)
    {
        if (al.get(i).start > time_limit)
        {
             
            // Add selected meeting to arraylist
            m.add(al.get(i).pos);
             
            // Update time limit
            time_limit = al.get(i).end;
        }
    }
     
    // Print final selected meetings.
     for(int i = 0; i < m.size(); i++)
        System.out.print(m.get(i) + 1 + " ");
}
 
// Driver Code 
public static void main (String[] args)
{
     
    // Starting time
    int s[] = { 1, 3, 0, 5, 8, 5 };
     
    // Finish time
    int f[] = { 2, 4, 6, 7, 9, 9 }; 
     
    // Defining an arraylist of meet type
    ArrayList meet = new ArrayList<>();
    for(int i = 0; i < s.length; i++)
     
        // Creating object of meeting
        // and adding in the list
        meet.add(new meeting(s[i], f[i], i));
         
    // Function call
    maxMeeting(meet, meet.size());
}
}
 
// This code is contributed by Sarthak Sethi


Python3
# Python3 program to find maximum number
# of meetings
 
# Custom class for storing starting time,
# finishing time and position of meeting.
class meeting:
     
    def __init__(self, start, end, pos):
         
        self.start = start
        self.end = end
        self.pos = pos
 
# Function for finding maximum
# meeting in one room
def maxMeeting(l, n):
 
    # Initialising an arraylist
    # for storing answer
    ans = []
     
    # Sorting of meeting according to
    # their finish time.
    l.sort(key = lambda x: x.end)
 
    # Initially select first meeting
    ans.append(l[0].pos)
 
    # time_limit to check whether new
    # meeting can be conducted or not.
    time_limit = l[0].end
     
    # Check for all meeting whether it
    # can be selected or not.
    for i in range(1, n):
        if l[i].start > time_limit:
            ans.append(l[i].pos)
            time_limit = l[i].end
             
    # Print final selected meetings
    for i in ans:
        print(i + 1, end = " ")
         
    print()
 
# Driver code
if __name__ == '__main__':
     
    # Starting time
    s = [ 1, 3, 0, 5, 8, 5 ]
 
    # Finish time
    f = [ 2, 4, 6, 7, 9, 9 ]
 
    # Number of meetings.
    n = len(s)
 
    l = []
 
    for i in range(n):
         
        # Creating object of meeting
        # and adding in the list
        l.append(meeting(s[i], f[i], i))
         
    # Function call
    maxMeeting(l, n)
 
# This code is contributed by MuskanKalra1


输出:

1 2 4 5

时间复杂度: O(N log(N))

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