jayati

Minimum Platforms

Aug 11th, 2023
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.38 KB | None | 0 0
  1. //{ Driver Code Starts
  2. // Program to find minimum number of platforms
  3. // required on a railway station
  4. #include <bits/stdc++.h>
  5. using namespace std;
  6.  
  7.  
  8. // } Driver Code Ends
  9. class Solution{
  10.     public:
  11.     //Function to find the minimum number of platforms required at the
  12.     //railway station such that no train waits.
  13.     int findPlatform(int arr[], int dep[], int n)
  14.     {
  15.         // Your code here
  16.         vector<pair<int,int>> v(n);
  17.         for(int i=0;i<n;i++)
  18.         {
  19.             v[i]={arr[i],dep[i]};
  20.         }
  21.         sort(v.begin(),v.end());
  22.        
  23.         priority_queue<int,vector<int>,greater<int>> q;
  24.         q.push(v[0].second);
  25.         int c=1;
  26.         for(int i=1;i<n;i++)
  27.         {
  28.             if(q.top()>=v[i].first)
  29.             {
  30.                
  31.                 c++;
  32.             }
  33.             else
  34.             {
  35.                 q.pop();
  36.             }
  37.             q.push(v[i].second);
  38.         }
  39.         return c;
  40.     }
  41. };
  42.  
  43.  
  44. //{ Driver Code Starts.
  45. // Driver code
  46. int main()
  47. {
  48.     int t;
  49.     cin>>t;
  50.     while(t--)
  51.     {
  52.         int n;
  53.         cin>>n;
  54.         int arr[n];
  55.         int dep[n];
  56.         for(int i=0;i<n;i++)
  57.             cin>>arr[i];
  58.         for(int j=0;j<n;j++){
  59.             cin>>dep[j];
  60.         }
  61.         Solution ob;
  62.         cout <<ob.findPlatform(arr, dep, n)<<endl;
  63.     }
  64.    return 0;
  65. }
  66. // } Driver Code Ends
  67. https://practice.geeksforgeeks.org/problems/minimum-platforms-1587115620/
Add Comment
Please, Sign In to add comment