Created
May 24, 2021 02:45
-
-
Save sauravgpt/37326b56936f6de4fa526df7b23cdf49 to your computer and use it in GitHub Desktop.
Activity Selection Problem (N meetings in one room)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int maxMeetings(int start[], int end[], int n) { | |
vector<int> order(n); | |
iota(order.begin(), order.end(), 0); | |
sort(order.begin(), order.end(), [&](int a, int b) -> bool { | |
return end[a] < end[b]; | |
}); | |
pair<int, int> last = {start[order[0]], end[order[0]]}; | |
int ans = 1; | |
for(int i=1; i<n; i++) { | |
int s = start[order[i]]; | |
int f = end[order[i]]; | |
if(last.second < s) { | |
last = {s, f}; | |
ans += 1; | |
} | |
} | |
return ans; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment