|
| 1 | +class Solution { |
| 2 | + public: |
| 3 | + |
| 4 | + // Function to find the maximum number of meetings that can be scheduled |
| 5 | + int maxMeetings(vector<int>& start, vector<int>& end) { |
| 6 | + int n = start.size(); // Get the number of meetings |
| 7 | + |
| 8 | + // Vector to store pairs of (start time, end time) for each meeting |
| 9 | + vector<pair<int, int>> v; |
| 10 | + |
| 11 | + // Populate the vector with pairs of start and end times |
| 12 | + for(int i = 0; i < n; i++) { |
| 13 | + pair<int, int> p = make_pair(start[i], end[i]); // Create a pair of start and end time |
| 14 | + v.push_back(p); // Add the pair to the vector |
| 15 | + } |
| 16 | + |
| 17 | + // Sort the meetings based on their end times |
| 18 | + sort(v.begin(), v.end(), [](pair<int, int> a, pair<int, int> b) { |
| 19 | + return a.second < b.second; // Sort by the second element (end time) of the pair |
| 20 | + }); |
| 21 | + |
| 22 | + // Initialize count of selected meetings and set the end time of the first meeting |
| 23 | + int count = 1; // The first meeting is always selected |
| 24 | + int ansEnd = v[0].second; // Set the end time of the first meeting |
| 25 | + |
| 26 | + // Iterate through the remaining meetings |
| 27 | + for(int i = 1; i < n; i++) { |
| 28 | + // If the start time of the current meeting is greater than the end time of the last selected meeting |
| 29 | + if(v[i].first > ansEnd) { |
| 30 | + count++; // Increment the count of selected meetings |
| 31 | + ansEnd = v[i].second; // Update the end time to the current meeting's end time |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + // Return the maximum number of non-overlapping meetings |
| 36 | + return count; |
| 37 | + } |
| 38 | +}; |
0 commit comments