|
| 1 | +<h1 align="center">Maximum - Meetings in - One Room</h1> |
| 2 | + |
| 3 | +## Problem Statement |
| 4 | + |
| 5 | +**Problem URL :** [Maximum Meetings in One Room](https://www.geeksforgeeks.org/problems/maximum-meetings-in-one-room/0) |
| 6 | + |
| 7 | + |
| 8 | + |
| 9 | +### Problem Explanation |
| 10 | +The problem is about scheduling the maximum number of meetings in a single meeting room. Each meeting has a start time and an end time. The goal is to select as many non-overlapping meetings as possible and return the indices of the selected meetings in ascending order. |
| 11 | + |
| 12 | +#### Example Input: |
| 13 | + |
| 14 | +1. **Number of meetings**: |
| 15 | + ( n = 6 ) |
| 16 | + |
| 17 | +2. **Start times**: |
| 18 | + ( S = [1, 3, 0, 5, 8, 5] ) |
| 19 | + |
| 20 | +3. **End times**: |
| 21 | + ( F = [2, 4, 6, 7, 9, 9] ) |
| 22 | + |
| 23 | +#### Expected Output: |
| 24 | + |
| 25 | +Indices of meetings that can be attended: |
| 26 | +[ textbf{[1, 2, 4, 5]} ] |
| 27 | + |
| 28 | + |
| 29 | +### Greedy Algorithm Explanation |
| 30 | + |
| 31 | +The **Greedy Algorithm** is used to solve the problem by prioritizing meetings that end earliest to maximize the number of meetings that can be attended. |
| 32 | + |
| 33 | + |
| 34 | +#### Steps of the Greedy Algorithm: |
| 35 | + |
| 36 | +1. **Input Preparation**: |
| 37 | + Create a list of meetings where each meeting is represented by its start time, end time, and original index. |
| 38 | + |
| 39 | + Example: |
| 40 | + ``` |
| 41 | + v = [({1, 2}, 1), ({3, 4}, 2), ({0, 6}, 3), ({5, 7}, 4), ({8, 9}, 5), ({5, 9}, 6)] |
| 42 | + ``` |
| 43 | + |
| 44 | +2. **Sort Meetings**: |
| 45 | + Sort meetings by their end times in ascending order. If two meetings have the same end time, sort them by their start times. |
| 46 | + |
| 47 | + Sorted list: |
| 48 | + ``` |
| 49 | + v = [({1, 2}, 1), ({3, 4}, 2), ({5, 7}, 4), ({0, 6}, 3), ({8, 9}, 5), ({5, 9}, 6)] |
| 50 | + ``` |
| 51 | + |
| 52 | +3. **Select Meetings**: |
| 53 | + - Initialize an empty list `ans` to store the indices of selected meetings. |
| 54 | + - Add the first meeting to the result and track its end time. |
| 55 | + - For each subsequent meeting, check if its start time is greater than the end time of the last selected meeting. If yes, select the meeting and update the end time. |
| 56 | + |
| 57 | + Selection process: |
| 58 | + - **Meeting 1**: Add (index 1) → `endAns = 2` |
| 59 | + - **Meeting 2**: Add (index 2) → `endAns = 4` |
| 60 | + - **Meeting 4**: Add (index 4) → `endAns = 7` |
| 61 | + - **Meeting 5**: Add (index 5) → `endAns = 9` |
| 62 | + |
| 63 | + Selected meetings: |
| 64 | + ``` |
| 65 | + ans = [1, 2, 4, 5] |
| 66 | + ``` |
| 67 | + |
| 68 | +4. **Sort Results**: |
| 69 | + Sort the selected indices in ascending order. |
| 70 | + |
| 71 | + Final result: |
| 72 | + ``` |
| 73 | + ans = [1, 2, 4, 5] |
| 74 | + ``` |
| 75 | + |
| 76 | +## Problem Solution |
| 77 | +```cpp |
| 78 | +class Solution { |
| 79 | +public: |
| 80 | + // Function to find the order of meetings that can be scheduled |
| 81 | + // without overlapping, maximizing the number of meetings. |
| 82 | + vector<int> maxMeetings(int n, vector<int> &S, vector<int> &F) { |
| 83 | + vector<int> ans; // To store the result (order of meetings that can be scheduled) |
| 84 | + |
| 85 | + // Vector to store pairs of (start time, end time) with their original index |
| 86 | + vector<pair<pair<int, int>, int>> v; |
| 87 | + |
| 88 | + // Populate the vector with pairs of start and end times along with the meeting index |
| 89 | + for (int i = 0; i < n; i++) { |
| 90 | + v.push_back({{S[i], F[i]}, i + 1}); // Store the start time, end time, and meeting index (1-based) |
| 91 | + } |
| 92 | + |
| 93 | + // Sort the vector of meetings: |
| 94 | + // 1. First by end time (ascending). |
| 95 | + // 2. If two meetings have the same end time, sort by start time (ascending). |
| 96 | + sort(v.begin(), v.end(), [](const pair<pair<int, int>, int>& a, const pair<pair<int, int>, int>& b) { |
| 97 | + if (a.first.second == b.first.second) |
| 98 | + return a.first.first < b.first.first; // Sort by start time if end times are equal |
| 99 | + return a.first.second < b.first.second; // Otherwise, sort by end time |
| 100 | + }); |
| 101 | + |
| 102 | + // Select the first meeting (greedy choice) |
| 103 | + ans.push_back(v[0].second); // Add the index of the first meeting to the result |
| 104 | + int endAns = v[0].first.second; // Store the end time of the first selected meeting |
| 105 | + |
| 106 | + // Iterate through the remaining meetings to find non-overlapping meetings |
| 107 | + for (int i = 1; i < n; i++) { |
| 108 | + // Check if the start time of the current meeting is greater than |
| 109 | + // the end time of the last selected meeting |
| 110 | + if (v[i].first.first > endAns) { |
| 111 | + ans.push_back(v[i].second); // Add the meeting index to the result |
| 112 | + endAns = v[i].first.second; // Update the end time to the current meeting's end time |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + // Sort the result to return the meeting indices in ascending order |
| 117 | + sort(ans.begin(), ans.end()); |
| 118 | + |
| 119 | + return ans; // Return the order of meeting indices that can be scheduled |
| 120 | + } |
| 121 | +}; |
| 122 | +``` |
| 123 | +
|
| 124 | +## Problem Solution Explanation |
| 125 | +
|
| 126 | +```cpp |
| 127 | +class Solution { |
| 128 | +public: |
| 129 | + vector<int> maxMeetings(int n, vector<int> &S, vector<int> &F) { |
| 130 | + vector<int> ans; // To store the result (order of meetings that can be scheduled) |
| 131 | + vector<pair<pair<int, int>, int>> v; // To store (start time, end time, index) |
| 132 | +``` |
| 133 | + |
| 134 | +- `ans`: Stores indices of selected meetings. |
| 135 | +- `v`: Stores meeting information as ((text{start time}, text{end time}, text{index})). |
| 136 | + |
| 137 | + |
| 138 | +```cpp |
| 139 | + // Populate vector `v` with start times, end times, and their original indices. |
| 140 | + for (int i = 0; i < n; i++) { |
| 141 | + v.push_back({{S[i], F[i]}, i + 1}); |
| 142 | + } |
| 143 | +``` |
| 144 | +
|
| 145 | +- Loop iterates through all meetings. |
| 146 | +- Push each meeting's start time, end time, and index (1-based) into `v`. |
| 147 | +
|
| 148 | +Example after population: |
| 149 | +``` |
| 150 | +v = [({1, 2}, 1), ({3, 4}, 2), ({0, 6}, 3), ({5, 7}, 4), ({8, 9}, 5), ({5, 9}, 6)] |
| 151 | +``` |
| 152 | +
|
| 153 | +
|
| 154 | +```cpp |
| 155 | + // Sort meetings by end time. If end times are equal, sort by start time. |
| 156 | + sort(v.begin(), v.end(), [](const pair<pair<int, int>, int>& a, const pair<pair<int, int>, int>& b) { |
| 157 | + if (a.first.second == b.first.second) |
| 158 | + return a.first.first < b.first.first; |
| 159 | + return a.first.second < b.first.second; |
| 160 | + }); |
| 161 | +``` |
| 162 | + |
| 163 | +- Meetings are sorted by: |
| 164 | + - End time (ascending). |
| 165 | + - If two meetings have the same end time, they are sorted by start time. |
| 166 | + |
| 167 | +Sorted `v`: |
| 168 | +``` |
| 169 | +v = [({1, 2}, 1), ({3, 4}, 2), ({5, 7}, 4), ({0, 6}, 3), ({8, 9}, 5), ({5, 9}, 6)] |
| 170 | +``` |
| 171 | + |
| 172 | + |
| 173 | +```cpp |
| 174 | + // Add the first meeting to the result |
| 175 | + ans.push_back(v[0].second); |
| 176 | + int endAns = v[0].first.second; |
| 177 | +``` |
| 178 | + |
| 179 | +- Add the first meeting (index 1) to the result. |
| 180 | +- `endAns` tracks the end time of the last selected meeting. |
| 181 | + |
| 182 | + |
| 183 | +```cpp |
| 184 | + // Iterate through remaining meetings |
| 185 | + for (int i = 1; i < n; i++) { |
| 186 | + if (v[i].first.first > endAns) { |
| 187 | + ans.push_back(v[i].second); |
| 188 | + endAns = v[i].first.second; |
| 189 | + } |
| 190 | + } |
| 191 | +``` |
| 192 | +
|
| 193 | +- For each meeting, check if its start time is greater than `endAns`. |
| 194 | +- If yes, add the meeting's index to `ans` and update `endAns`. |
| 195 | +
|
| 196 | +
|
| 197 | +```cpp |
| 198 | + // Sort the result indices |
| 199 | + sort(ans.begin(), ans.end()); |
| 200 | + |
| 201 | + return ans; |
| 202 | + } |
| 203 | +}; |
| 204 | +``` |
| 205 | + |
| 206 | +- Sort the indices in ascending order. |
| 207 | +- Return the final result. |
| 208 | + |
| 209 | + |
| 210 | +### Example Walkthrough |
| 211 | + |
| 212 | +#### Input: |
| 213 | +( n = 6, S = [1, 3, 0, 5, 8, 5], F = [2, 4, 6, 7, 9, 9] ) |
| 214 | + |
| 215 | +#### Execution: |
| 216 | +1. Populate ( v ): |
| 217 | + ``` |
| 218 | + [({1, 2}, 1), ({3, 4}, 2), ({0, 6}, 3), ({5, 7}, 4), ({8, 9}, 5), ({5, 9}, 6)] |
| 219 | + ``` |
| 220 | + |
| 221 | +2. Sort ( v ): |
| 222 | + ``` |
| 223 | + [({1, 2}, 1), ({3, 4}, 2), ({5, 7}, 4), ({0, 6}, 3), ({8, 9}, 5), ({5, 9}, 6)] |
| 224 | + ``` |
| 225 | + |
| 226 | +3. Select meetings: |
| 227 | + ``` |
| 228 | + ans = [1, 2, 4, 5] |
| 229 | + ``` |
| 230 | + |
| 231 | +4. Sort ( ans ): |
| 232 | + ``` |
| 233 | + [1, 2, 4, 5] |
| 234 | + ``` |
| 235 | + |
| 236 | +#### Output: |
| 237 | +``` |
| 238 | +[1, 2, 4, 5] |
| 239 | +``` |
| 240 | + |
| 241 | + |
| 242 | +### Time and Space Complexity |
| 243 | + |
| 244 | +1. **Time Complexity**: |
| 245 | + - Sorting meetings: ( O(n log n) ). |
| 246 | + - Iterating through meetings: ( O(n) ). |
| 247 | + Total: ( O(n log n) ). |
| 248 | + |
| 249 | +2. **Space Complexity**: |
| 250 | + - Auxiliary space for vector ( v ): ( O(n) ). |
| 251 | + Total: ( O(n) ). |
0 commit comments