contact me at [email protected] link
I came across this video: https://www.youtube.com/watch?v=INauRP_Ovtk in my recommended one day.
I watched the first part, which if I understood correctly, the problem is:
- scheduling meetings(or interviews)
- meetings have a duration.
- get the minimum amount of rooms for set amount of meetings.
they had this example to show:
I thought I could do better without using trees or complicated things.
Here was my initial thought: make an int array with length of 24, and set all of its members to 0. each index will be considered every hour of a day. When a new meeting is entered, increment that index’s amount by 1. For example, if a new meeting is entered from 2pm~4pm, array index 14 to 16 will be incremented by 1. Repeat the process and get the maximum value in the array.
here’s my take in cpp
class Meeting {
int startTime;
int endTime;
public:
Meeting() {
startTime = 0;
endTime = 0;
}
Meeting(int startTime, int endTime) {
this->startTime = startTime;
this->endTime = endTime;
}
// get start time
int getStartTime() {
return startTime;
}
// get end time
int getEndTime() {
return endTime;
}
};
int main()
{
int time[24] = {0};
std::cout << "Enter the number of meetings: ";
int n;
std::cin >> n;
Meeting *meetings = new Meeting[n];
for (int i = 0; i < n; i++) {
std::cout << "Enter the start time and end time of meeting " << i + 1 << ": ";
int startTime, endTime;
std::cin >> startTime >> endTime;
meetings[i] = Meeting(startTime, endTime);
}
for (int i = 0; i < n; i++) {
int startTime = meetings[i].getStartTime();
int endTime = meetings[i].getEndTime();
for (int j = startTime; j < endTime; j++) {
time[j]++;
}
}
int max = 0;
for (int i = 0; i < 24; i++) {
if (time[i] > max) {
max = time[i];
}
}
std::cout << "Maximum number of meetings that can be held in a day(Minimum amount of room required): " << max << std::endl;
return 0;
}
This approach might have a couple of issues:
The first one is the case where one meeting starts immediately after another one ends. For example, one meeting might start at 9:00 to 11:00 and another at 11:00 to 12:00. In that case, in my algorithm, the output will require 2 rooms at exactly 11:00. However some clients might want a seemless transitions between meetings, so when the first meeting ends at 11, the next meeting can start right after. This algorithm can’t do that in this state. But I would argue that in reality, meetings rarely end at exactly when it should, so this method would compensate for that extra time if needed.
The second one is it is going to be quite messy when you want to do fractions of an hour. Maybe later, the user might want a 30 minute intervals between meetings, like 10:30 to 12:30. In that case, you would have to adjust the array size and the indexing should be recalculated, which is doable, but it would not be a clean job.
I guess it is fun to do these little challenges from time to time to keep my skills intact. I’ll do more when i find more good quizzes like this.