N meetings in one room | Practice | GeeksforGeeks

There is one meeting room in a firm. There are N meetings in the form of (start[i], end[i]) where start[i] is start time of meeting i and end[i] is finish time of meeting i.
What is the maximum number of meetings that can be accommodated in the meeting room when only one meeting can be held in the meeting room at a particular time? 
Note: Start time of one chosen meeting can't be equal to the end time of the other chosen meeting.

Example 1:
Input:
N = 6
start[] = {1,3,0,5,8,5}
end[] =  {2,4,6,7,9,9}
Output: 
4
Explanation:
Maximum four meetings can be held with
given start and end timings.
The meetings are - (1, 2),(3, 4), (5,7) and (8,9)

Example 2:
Input:
N = 3
start[] = {10, 12, 20}
end[] = {20, 25, 30}
Output: 
1
Explanation:
Only one meetings can be held
with given start and end timings.

Your Task :
You don't need to read inputs or print anything. Complete the function maxMeetings() that takes two arrays start[] and end[] along with their size N as input parameters and returns the maximum number of meetings that can be held in the meeting room.

Expected Time Complexity : O(N*LogN)
Expected Auxilliary Space : O(N)

Constraints:
1 ≤ N ≤ 105
0 ≤ start[i] < end[i] ≤ 105
0
dwivedisatyam12348 hours ago
python code 
 
       res=[]       for i in range(n):           res.append([start[i],end[i]])       res.sort(key=lambda x:(x[1],x[0]))       ans,j,i=1,1,0       for j in range(n):           if res[j][0]>res[i][1]:               ans+=1               i=j       return ans
0
gd29843 days ago
    int maxMeetings(int start[], int end[], int n)   {       // Your code here       if(n==0)           return 0;       vector<vector<int>> vec;              for(int i{};i<n;i++){           vector<int> v;           v.push_back(end[i]);           v.push_back(start[i]);           // v.push_back(i+1);           vec.push_back(v);       }       sort(vec.begin(), vec.end());       int limit = vec[0][0];       int count{1};       for(int i{1};i<vec.size();i++){           if(vec[i][1]>limit){               limit = vec[i][0];               count++;           }       }       return count;   }
//east or west vector is the best
0
sikkusaurav1234 days ago
int maxMeetings(int start[], int end[], int n)   {       // Your code here       vector<pair<int,int>>v;       int starting_point,ending_point;       int counting=1;       for(int i=0;i<n;i++)       {           v.push_back(make_pair(end[i],start[i]));       }       sort(v.begin(),v.end());        starting_point=v[0].second;        ending_point=v[0].first;       for(int i=1;i<n;i++)       {           if(v[i].second>ending_point)           {               ending_point=v[i].first;              counting++;                          }       }       return counting;   }
0
dhruvpurwar155 days ago
// { Driver Code Starts
#include <bits/stdc++.h>
using namespace std;

 // } Driver Code Ends
class Solution
{
    public:
    //Function to find the maximum number of meetings that can
    //be performed in a meeting room.
     bool static cmp(pair<int,int>a, pair<int,int>b){
        if(a.second==b.second){
            return a.first>b.first;
        }
        return a.second<b.second;
    }
    int maxMeetings(int start[], int end[], int n)
    {
        // Your code here
        vector<pair<int,int>>vec(n);
        for(int i=0;i<n;i++){
            vec[i].first=start[i];
            vec[i].second= end[i];
        }
        
        // for(auto x: vec){
        //     cout<<x.first<<" "<<x.second<<endl;
        // }
        
        sort(vec.begin(), vec.end(), cmp);
        int res=1;
        int prev=0;
        for(int i=1;i<n;i++){
            if(vec[i].first>vec[prev].second){
                res++;
                prev=i;
            }
        }
        
        return res;
    }
};

// { Driver Code Starts.
int main() {
    int t;
    cin >> t;
    while (t--) {
        int n;
        cin >> n;
        int start[n], end[n];
        for (int i = 0; i < n; i++) cin >> start[i];

        for (int i = 0; i < n; i++) cin >> end[i];

        Solution ob;
        int ans = ob.maxMeetings(start, end, n);
        cout << ans << endl;
    }
    return 0;
}  // } Driver Code Ends
0
dhruvpurwar155 days ago
ans.push_back(totalJobs);
0
doshividhi0215 days ago
def maximumMeetings(self,n,start,end):       # code here       meetings = list()       count = 1       for i in range(len(start)):           meet = list()           meet.append(start[i])           meet.append(end[i])           meetings.append(meet)       sorted(meetings,key=lambda x:x[1])       limit = meetings[0][1]       for i in range(1,len(start)):           if(meetings[i][0] >= limit):               limit = meetings[i][1]               count +=1                      return count
 
 
What's wrong in this?
0
ahmedxahoor721 week ago
int maxMeetings(int start[], int end[], int n)   {       // Your code here       vector<pair<int,int>>v;       for(int i=0;i<n;i++){           v.push_back({end[i],start[i]});                  }       sort(v.begin(),v.end());       int c=1;       int s=v[0].second,e=v[0].first;       for(int i=1;i<v.size();i++){           if(v[i].second>e){               c++;               e=v[i].first;           }       }       return c;          }
0
pruthvibelgaonkar4691 week ago
TIME TAKEN 0.55 SECONDS
 
 bool static comp(pair<int,int>a,pair<int,int>b)   {       if(a.second==b.second) return a.first<b.first;       return a.second<b.second;   }      int maxMeetings(int start[], int end[], int n)   {       vector<pair<int,int>>v(n);       for(int i=0;i<n;i++)       {           v[i]={start[i],end[i]};       }       sort(v.begin(),v.end(),comp);       int i=0;       int j=1;       int c=1;       while(j<n)       {           if(v[i].second<v[j].first)           {               c++;               i=j;               j++;           }           else           {               j++;           }       }       return c;   }
+1
shreyas172 weeks ago
// { Driver Code Starts
#include <bits/stdc++.h>
using namespace std;

 // } Driver Code Ends
class Solution
{
    public:
    //Function to find the maximum number of meetings that can
    //be performed in a meeting room.
    static bool secc(const pair<int ,int>&a , const pair<int,int>&b){
        return a.second < b.second ; 
    }
    int maxMeetings(int start[], int end[], int n)
    {
        vector<pair<int,int>>vp ;  
        for(int i=0;i<n;++i){
            vp.push_back({start[i],end[i]})  ; 
        }
        
        sort(vp.begin(),vp.end(),secc)  ; 
        
        int total = 0 ; 
        int p = 0  ; 
        
        for(int i=1;i<n;i++){
            if(vp[p].second >= vp[i].first){
                continue;  
            }
            else{
                total++ ; 
                p = i  ; 
            }
        }
        return total+1 ; 
        
    }
};

// { Driver Code Starts.
int main() {
    int t;
    cin >> t;
    while (t--) {
        int n;
        cin >> n;
        int start[n], end[n];
        for (int i = 0; i < n; i++) cin >> start[i];

        for (int i = 0; i < n; i++) cin >> end[i];

        Solution ob;
        int ans = ob.maxMeetings(start, end, n);
        cout << ans << endl;
    }
    return 0;
}  // } Driver Code Ends
0
jainmuskan5652 weeks ago
    //be performed in a meeting room.   static bool comp(pair<int,int> a, pair<int,int>b){       if(a.second== b.second){           return a.first<b.first;       }       return a.second<b.second;   }   int maxMeetings(int start[], int end[], int n)   {       // greedy       // sort wrt the finish time       vector<pair<int,int>> act(n);       for(int i=0;i<n;i++){           act[i]= {start[i], end[i]};       }       sort(act.begin(),act.end(), comp);       int i=0, j=1, count=1;       while(j<n){           if(act[i].second<act[j].first){               i=j;               count++;           }           j++;       }       return count;   }
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.