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.
- Prepare
- C++
- Other Concepts
- Attending Workshops
- Discussions
Attending Workshops
Attending Workshops
Sort by
recency
|
189 Discussions
|
Please Login in order to post a comment
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;
}
My solution:
include
using namespace std;
//Define the structs Workshops and Available_Workshops. struct Workshop { int st; int dur; int et; };
struct Available_Workshops { int n; vector wsarr; };
//Implement the functions initialize and CalculateMaxWorkshops Available_Workshops* initialize(int start_time[], int duration[], int n) { Available_Workshops *ws_avail = new Available_Workshops;
}
int CalculateMaxWorkshops(Available_Workshops* ws_avail) { int cnt = 1; int cur; vector vec;
}
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];
}
//try this
include
include
include
using namespace std;
class Workshop { public: int start_time; int end_time;
};
class Available_Workshops { public: vector workshops;
};
Available_Workshops* initialize(int start_time[], int duration[], int n) { vector workshops; for (int i = 0; i < n; i++) { workshops.push_back(Workshop(start_time[i], duration[i])); } Available_Workshops* available_workshops = new Available_Workshops(workshops); return available_workshops; }
int CalculateMaxWorkshops(Available_Workshops* ptr) { vector& workshops = ptr->workshops;
}
int main() { int n; // Number of workshops cin >> n;
}
I just tried a new solution in C++. I found that using a deque with workshops sorted by start time and duration is more convenient. First, store all workshops inside that deque, sort them, and then use a helper deque to get the maximum number of workshops with no overlap. The trick is to take the workshop with the earliest end time if they overlap. My code works and passed all tests successfully; however, it can be further improved.