jayati

Activity Selection

Oct 25th, 2023
825
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.33 KB | None | 0 0
  1. //{ Driver Code Starts
  2. #include<bits/stdc++.h>
  3. using namespace std;
  4.  
  5.  
  6. // } Driver Code Ends
  7. class Solution
  8. {
  9.     public:
  10.     //Function to find the maximum number of activities that can
  11.     //be performed by a single person.
  12.     static bool cmp(pair<int,int> a,pair<int,int> b)
  13.     {
  14.         return a.second<b.second;
  15.     }
  16.     int activitySelection(vector<int> start, vector<int> end, int n)
  17.     {
  18.  
  19.         vector<pair<int,int>> v;
  20.         for(int i=0;i<n;i++)
  21.         {
  22.             v.push_back({start[i],end[i]});
  23.         }
  24.         sort(v.begin(),v.end(),cmp);
  25.        
  26.         int prevend=v[0].second;
  27.         int c=1;
  28.         for(int i=1;i<n;i++)
  29.         {
  30.             if(v[i].first>prevend)
  31.             {
  32.                 c++;
  33.                 prevend=v[i].second;
  34.             }
  35.            
  36.         }
  37.         return c;
  38.     }
  39. };
  40.  
  41. //{ Driver Code Starts.
  42. int main()
  43. {
  44.     int t;
  45.    
  46.     //testcases
  47.     cin >> t;
  48.     while(t--)
  49.     {
  50.         //size of array
  51.         int n;
  52.         cin >> n;
  53.         vector<int> start(n), end(n);
  54.        
  55.         //adding elements to arrays start and end
  56.         for(int i=0;i<n;i++)
  57.             cin>>start[i];
  58.         for(int i=0;i<n;i++)
  59.             cin>>end[i];
  60.         Solution ob;
  61.         //function call
  62.         cout << ob.activitySelection(start, end, n) << endl;
  63.     }
  64.     return 0;
  65. }
  66.  
  67. // } Driver Code Ends
Advertisement
Add Comment
Please, Sign In to add comment