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 Workshop {
int start_time;
int duration;
int end_time;
};
struct Available_Workshops {
int n;
vector workshops;
};
Available_Workshops* initialize(int start_time[], int duration[], int n) {
Available_Workshops* aw = new Available_Workshops();
aw->n = n;
for (int i = 0; i < n; ++i) {
Workshop ws;
ws.start_time = start_time[i];
ws.duration = duration[i];
ws.end_time = start_time[i] + duration[i];
aw->workshops.push_back(ws);
}
return aw;
}
int CalculateMaxWorkshops(Available_Workshops* ptr) {
vector<Workshop>& workshops = ptr->workshops;
sort(workshops.begin(), workshops.end(), [](const Workshop& a, const Workshop& b) {
return a.end_time < b.end_time;
});
int count = 0;
int last_end_time = 0;
for (const auto& ws : workshops) {
if (ws.start_time >= last_end_time) {
++count;
last_end_time = ws.end_time;
}
}
return count;
}
my solution(cp20++):
int main() {
int n;
cin >> n;
int start_time[n], duration[n];
for (int i = 0; i < n; ++i) cin >> start_time[i];
for (int i = 0; i < n; ++i) cin >> duration[i];
Available_Workshops* aw = initialize(start_time, duration, n);
cout << CalculateMaxWorkshops(aw) << endl;
return 0;
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Attending Workshops
You are viewing a single comment's thread. Return to all comments →
include
include
include
using namespace std;
struct Workshop { int start_time; int duration; int end_time; };
struct Available_Workshops { int n;
vector workshops; };
Available_Workshops* initialize(int start_time[], int duration[], int n) { Available_Workshops* aw = new Available_Workshops(); aw->n = n; for (int i = 0; i < n; ++i) { Workshop ws; ws.start_time = start_time[i]; ws.duration = duration[i]; ws.end_time = start_time[i] + duration[i]; aw->workshops.push_back(ws); } return aw; }
int CalculateMaxWorkshops(Available_Workshops* ptr) {
}
my solution(cp20++): int main() { int n; cin >> n;
}