Skip to content

Instantly share code, notes, and snippets.

@sauravgpt
Created May 24, 2021 02:45
Show Gist options
  • Save sauravgpt/37326b56936f6de4fa526df7b23cdf49 to your computer and use it in GitHub Desktop.
Save sauravgpt/37326b56936f6de4fa526df7b23cdf49 to your computer and use it in GitHub Desktop.
Activity Selection Problem (N meetings in one room)
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