In this HackerRank Attending Workshops problem in c++ we need to implement two structures and two functions so that a student can sign up for maximum workshops where no two workshops overlap.

HackerRank Attending Workshops in C++ problem solution

 HackerRank Attending Workshops in C++ problem solution

struct Workshop
{
	int start_time;
	int end_time;
	int duration;

	bool operator<(const Workshop& w) { return end_time < w.end_time; }
};

struct Available_Workshops
{
	Available_Workshops(int num_workshops) : m_num_workshops(num_workshops)
	{
		m_workshops = new Workshop[num_workshops];
	}

	~Available_Workshops() { delete[] m_workshops; }

	int m_num_workshops;
	Workshop *m_workshops;
};


Available_Workshops * initialize(int start_time[], int duration[], int N)
{
	Available_Workshops *aw = new Available_Workshops(N);

	for (int i = 0; i < N; i++)
	{
		Workshop w;
		w.start_time = start_time[i];
		w.duration = duration[i];
		w.end_time = w.start_time + w.duration;

		aw->m_workshops[i] = w;
	}

	return aw;
}

int CalculateMaxWorkshops(Available_Workshops *aw)
{
	int min_start = 10000000;
	std::vector<Workshop> workshops;
	for (int i = 0; i < aw->m_num_workshops; i++)
	{
		workshops.push_back(aw->m_workshops[i]);
		min_start = std::min(min_start, workshops[i].start_time);
	}

	// Find the minimum start time
	std::sort(workshops.begin(), workshops.end());

	int num = 0;
	int curs = min_start;

	for (int i = 0; i < workshops.size(); i++)
	{
		if (workshops[i].start_time >= curs)
		{
			num++;
			curs = workshops[i].end_time;
		}
	}

	return num;
}