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.
structWorkshop{intstart_time;intduration;intend_time;};structAvailable_Workshops{intn{0};Workshop*workshops;Available_Workshops(intn):n(n){workshops=newWorkshop[n];}~Available_Workshops(){delete[]workshops;workshops=nullptr;}};Available_Workshops*initialize(int*start_time,int*duration,intn){Available_Workshops*obj=newAvailable_Workshops(n);for(inti=0;i<n;++i){obj->workshops[i].start_time=start_time[i];obj->workshops[i].duration=duration[i];obj->workshops[i].end_time=start_time[i]+duration[i];}returnobj;}intCalculateMaxWorkshops(Available_Workshops*ptr){if(ptr==nullptr||ptr->workshops==nullptr||ptr->n<=0)return0;// Sort workshops based on their END timesstd::sort(ptr->workshops,ptr->workshops+ptr->n,[](constWorkshop&a,constWorkshop&b){returna.end_time<b.end_time;});intmaxWorkshops{0},endTime{0};for(inti{0};i<ptr->n;++i){if(ptr->workshops[i].start_time>=endTime){// Non-overlapping workshop found++maxWorkshops;endTime=ptr->workshops[i].end_time;}}returnmaxWorkshops;};
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Attending Workshops
You are viewing a single comment's thread. Return to all comments →