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
|
188 Discussions
|
Please Login in order to post a comment
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.