fork download
  1. /*
  2. Given an array of numeric pairs representing the start and end times of scheduled meetings, find the maximum number of meetings that can be held in a room if only one meeting can occur in the room at once.
  3. Example
  4. Input: [(8.5, 9.5), (11, 11.25), (9, 11), (11.5, 13), (10, 10.75), (8, 9)]
  5. Output: 4 (The following meetings can occur in the given order: (8, 9), (10, 10.75), (11, 11.25), 11.5, 13))
  6.  
  7. (8, 9)(8.5, 9.5)(9, 11)
  8.  
  9. */
  10.  
  11. #include <iostream>
  12. #include <vector>
  13. #include <algorithm>
  14. using namespace std;
  15.  
  16.  
  17. int maxmeetings(vector<pair<double,double>>& meetings){
  18.  
  19. sort(meetings.begin() , meetings.end(),
  20. [](auto &a , auto &b){
  21. return a.second < b.second;
  22. });
  23.  
  24. int count = 1;
  25. double lastend = meetings[0].second;
  26.  
  27. for(int i = 1 ; i<meetings.size() ; i++){
  28. if(meetings[i].first >= lastend){
  29. count++;
  30. lastend = meetings[i].second;
  31. }
  32. }
  33. return count;
  34. }
  35. int main() {
  36. vector<pair<double,double>> meetings = {{8, 8.5}, {11, 11.25}, {9, 11}, {11.5, 13}, {10, 10.75}, {8, 9}};
  37.  
  38. cout<< maxmeetings(meetings);
  39. return 0;
  40. }
Success #stdin #stdout 0s 5316KB
stdin
Standard input is empty
stdout
4