forked from rene-d/hackerrank
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathattending-workshops.cpp
88 lines (68 loc) · 1.94 KB
/
attending-workshops.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Attending Workshops
// Define a structure for the workshop and find the number of workshops that the student can attend.
//
// https://www.hackerrank.com/challenges/attending-workshops/problem
//
#include <bits/stdc++.h>
using namespace std;
// (skeliton_head) ----------------------------------------------------------------------
//Define the structs Workshops and Available_Workshops.
//Implement the functions initialize and CalculateMaxWorkshops
struct Workshops
{
int start_time = 0;
int duration = 0;
int end_time = 0;
Workshops(int s, int d) :
start_time(s), duration(d), end_time(s + d)
{
}
};
struct Available_Workshops
{
vector<Workshops> arr;
};
Available_Workshops* initialize (int start_time[], int duration[], int n)
{
Available_Workshops* aw = new Available_Workshops();
for (int i = 0; i < n; ++i)
{
aw->arr.push_back(Workshops(start_time[i], duration[i]));
}
return aw;
}
int CalculateMaxWorkshops(Available_Workshops *aw)
{
std::sort(aw->arr.begin(), aw->arr.end(),
[](const Workshops & a, const Workshops & b) -> bool
{
return a.end_time < b.end_time;
});
int last_time=-1;
int nb = 0;
for (const auto& w : aw->arr) {
if (w.start_time >= last_time) {
last_time = w.end_time;
nb++;
}
}
return nb;
}
// (skeliton_tail) ----------------------------------------------------------------------
int main(int argc, char *argv[]) {
int n; // number of workshops
cin >> n;
// create arrays of unknown size n
int* start_time = new int[n];
int* duration = new int[n];
for(int i=0; i < n; i++){
cin >> start_time[i];
}
for(int i = 0; i < n; i++){
cin >> duration[i];
}
Available_Workshops * ptr;
ptr = initialize(start_time,duration, n);
cout << CalculateMaxWorkshops(ptr) << endl;
return 0;
}