Activity Selection | Practice | GeeksforGeeks

Given N activities with their start and finish day given in array start[ ] and end[ ]. Select the maximum number of activities that can be performed by a single person, assuming that a person can only work on a single activity at a given day.
Note : Duration of the activity includes both starting and ending day.

Example 1:
Input:
N = 2
start[] = {2, 1}
end[] = {2, 2}
Output: 
1
Explanation:
A person can perform only one of the
given activities.

Example 2:
Input:
N = 4
start[] = {1, 3, 2, 5}
end[] = {2, 4, 3, 6}
Output: 
3
Explanation:
A person can perform activities 1, 2
and 4.


Your Task :
You don't need to read input or print anything. Your task is to complete the function activityselection() which takes array start[ ], array end[ ] and integer N as input parameters and returns the maximum number of activities that can be done.

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

Constraints:
1 ≤ N ≤ 2*105
1 ≤ start[i] ≤ end[i] ≤ 109
0
19003001002371 day ago
class Solution{   public:   static bool mycomp(vector<int>& a, vector<int>& b) {       return a[1] < b[1];   }   //Function to find the maximum number of activities that can   //be performed by a single person.   int activitySelection(vector<int> start, vector<int> end, int n)   {       // Your code here       vector<vector<int>> act;       for(int i=0;i<n;i++)            act.push_back({start[i] , end[i]});       sort(act.begin(),act.end(), mycomp);              int ans = 1,i=0;       for(int j=1;j<n;j++) {           if(act[j][0] > act[i][1]) {               ans++;               i=j;           }       }       return ans;   }};
0
ravi1010prakash2 days ago
class Solution{   public:   static bool mycamp(vector<int>&a,vector<int>&b)   {       return a[1]<b[1];   }   //Function to find the maximum number of activities that can   //be performed by a single person.   int activitySelection(vector<int> start, vector<int> end, int n)   {       vector<vector<int>>act;        for(int i=0;i<n;i++)       act.push_back({start[i],end[i]});       sort(act.begin(),act.end(), mycamp);       int count=1;       int i=0;       for(int j=1;j<n;j++)       {          if(act[i][1]<act[j][0]){           count++;           i=j;}       }       return count;   }};
// { Driver Code Starts.int main(){   int t;      //testcases   cin >> t;   while(t--)   {       //size of array       int n;       cin >> n;       vector<int> start(n), end(n);              //adding elements to arrays start and end       for(int i=0;i<n;i++)           cin>>start[i];       for(int i=0;i<n;i++)           cin>>end[i];       Solution ob;       //function call       cout << ob.activitySelection(start, end, n) << endl;   }   return 0;} // } Driver Code Ends
0
pruthvibelgaonkar4692 weeks ago
TIME TAKEN 0.5 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 activitySelection(vector<int> start, vector<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)           {               i=j;               j++;               c++;           }           else           {               j++;           }       }       return c;   }
0
dha72
This comment was deleted.
0
shalini21sirothiya3 weeks ago
//c++
int activitySelection(vector<int> start, vector<int> end, int n)   {       // Your code here      vector<vector<int>>ans;      for(int i=0;i<n;i++){          ans.push_back({start[i],end[i]});      }      sort(ans.begin(),ans.end(),[&](vector<int>&a,vector<int>&b){return a[1]<b[1];});      int take=1,prev=ans[0][1];      for(int i=1;i<n;i++){          if(ans[i][0]>prev){              take++;              prev=ans[i][1];          }      }       return take;   }
0
manishvaishnav911 month ago
Other Java solutions I found may not be easy for beginners to read and understand so here is one. 
 
class Solution
{
   static class Meeting implements Comparable<Meeting> {
       private int start,end;
       
       Meeting(int start,int end) {
           this.start = start;
           this.end = end;
       }
       
       public int getStart() {
           return this.start;
       }
       
       public int getEnd() {
           return this.end;
       }
       
       public int compareTo(Meeting other) {
           return this.end - other.end;
       }
   }
   
   public static int activitySelection(int start[], int end[], int n)
   {
       // add your code here
       List<Meeting> list = new ArrayList<>();
       for (int i = 0; i < end.length; i++) {
           list.add(new Meeting(start[i],end[i]));
       }
       Collections.sort(list);
       
       int count = 0;
       int prevStart = -1;
       int prevEnd = -1;
       
       for (Meeting m : list) {
           int e = m.getEnd();
           int s = m.getStart();
           if (prevStart < 0 && prevEnd < 0) {
               count++;
               prevStart = s;
               prevEnd = e;
           }
           else if (s > prevEnd) {
               count++;
               prevStart = s;
               prevEnd = e;
           }
       }
       return count;
   }
}
0
harrypotter01 month ago
    #Function to find the maximum number of activities that can   #be performed by a single person.   def activitySelection(self,n,start,end):       res = []       for i in range(n):           res.append([start[i], end[i]])       res.sort(key = lambda x:(x[1],x[0]))       temp,ans,j,i =1,1,1,0       while i<n and j<n:           if res[i][1]<res[j][0]:               temp+=1               # print(res[j][0], res[j][1])               i = j               j+=1           else:               j+=1       return temp 
0
naveenapatnaik1 month ago
JAVA SOLUTION!!! 
   public static int activitySelection(int start[], int end[], int n)   {       List<List<Integer>> activity = new ArrayList<>();       for(int i = 0 ; i < n; i++){           List<Integer> act = new ArrayList<>();           act.add(start[i]);           act.add(end[i]);           activity.add(act);       }       activity.sort(new Comparator<List<Integer>>() {       @Override       public int compare(List<Integer> o1, List<Integer> o2) {           if (o1.get(1) > o2.get(1)) {               return 1;           }           else if(o1.get(1) < o2.get(1)) {               return -1;           }        else if (o1.get(1).equals(o2.get(1)) &&            o1.get(0) > o2.get(0)) {               return 1;            }           else if(o1.get(1).equals(o2.get(1)) &&            o1.get(0) < o2.get(0)){               return -1;               }           return 0;       }});       int i = 0, res = 1;       for(int j = 1 ; j < n ; j++){           if(activity.get(j).get(0) > activity.get(i).get(1)){               i = j;               res++;           }       }       return res;   }
+1
patildhiren441 month ago
// Approach:// 1. Add details of meeting like start end and position with 1 based index to list// 2. sort the list acc to end time of list (if two meetings has same time then sort acc to their min pos)// 3. take max limit as first end of meeting and add the position to list// 4. if next meeting start time is greater than limit  (which is prev meeting end time) then add to another final list// 5. print final list as attend meeting position or how many meeting acc to que 
// Java - 1.4
 
!! Spoiler Alert Code Ahead
!! Spoiler Alert  Code Ahead
!! Spoiler Alert  Code Ahead
!! Spoiler Alert  Code Ahead
!! Spoiler Alert  Code Ahead
!! Spoiler Alert  Code Ahead
!! Spoiler Alert  Code Ahead
!! Spoiler Alert  Code Ahead
!! Spoiler Alert  Code Ahead
!! Spoiler Alert  Code Ahead
!! Spoiler Alert  Code Ahead
 
 
 
 
static class Meeting{
        int st;
        int end;
        int pos;
        Meeting(int st, int end, int pos){
            this.st = st;
            this.end = end;
            this.pos = pos;
        }
    }
    
    static class meetCompare implements Comparator<Meeting>{
        public int compare(Meeting m1, Meeting m2){
            if(m1.end < m2.end){
                return -1;
            }else if(m1.end > m2.end){
                return 1;
            }else if(m1.pos < m2.pos){
                return -1;
            }else{
                return 1;
            }
        }
    }
    
    public static int activitySelection(int start[], int end[], int n)
    {
        // add your code here
        ArrayList<Meeting> meet = new ArrayList<>();
        
        for(int i=0; i<n; i++){
            meet.add(new Meeting(start[i], end[i], i+1));
        }
        
        Collections.sort(meet, new meetCompare());
        
        ArrayList<Integer> res = new ArrayList<>();
        
        res.add(meet.get(0).pos);
        int limit = meet.get(0).end;
        
        for(int i=1; i<n; i++){
            if(meet.get(i).st > limit){
                limit = meet.get(i).end;
                res.add(meet.get(i).pos);
            }
        }
        
        // for(int i=0; i<res.size(); i++){
        //     System.out.println(res.get(i));
        // }
        
        return res.size();
    }
0
harinigurram2001
This comment was deleted.
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.