We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
struct Activity {
int start;
int finish;
int index; // Original index of the activity
};
bool compare(Activity a, Activity b) {
return a.finish < b.finish;
}
void maxActivities(vector& activities) {
int n = activities.size();
// Sort activities based on finish time
sort(activities.begin(), activities.end(), compare);
vector<int> selectedActivities;
selectedActivities.push_back(activities[0].index);
int prevFinish = activities[0].finish;
for (int i = 1; i < n; ++i) {
if (activities[i].start >= prevFinish) {
selectedActivities.push_back(activities[i].index);
prevFinish = activities[i].finish;
}
}
// Output
for (int i = 0; i < selectedActivities.size(); ++i) {
cout << selectedActivities[i] << "\t";
}
cout << endl;
}
int main() {
int n;
cin >> n;
vector<Activity> activities(n);
for (int i = 0; i < n; ++i) {
cin >> activities[i].start >> activities[i].finish;
activities[i].index = i + 1; // Assign original index
}
maxActivities(activities);
return 0;
}****
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Interval Selection
You are viewing a single comment's thread. Return to all comments →
include
include
include
using namespace std;
struct Activity { int start; int finish; int index; // Original index of the activity };
bool compare(Activity a, Activity b) { return a.finish < b.finish; }
void maxActivities(vector& activities) { int n = activities.size(); // Sort activities based on finish time sort(activities.begin(), activities.end(), compare);
}
int main() { int n; cin >> n;
}****