You are viewing a single comment's thread. Return to all comments →
It took me some time to figure it out. But this eventually worked and passed all the test cases.
struct Workshop { int start_time; int duration; int end_time; }; struct Available_Workshops { int n; map<int , Workshop> mpw; }; Available_Workshops * initialize(int s[], int d[], int n){ Available_Workshops* ptr = new Available_Workshops(); map<int, Workshop>::iterator itr; for(int i = 0; i < n; i++){ if((itr = ptr->mpw.find(s[i])) != ptr->mpw.end()){ if(ptr->mpw[s[i]].duration <= d[i]) continue; ptr->mpw[s[i]].duration = d[i]; ptr->mpw[s[i]].end_time = s[i] + d[i]; }else{ ptr->mpw[s[i]].start_time = s[i]; ptr->mpw[s[i]].duration = d[i]; ptr->mpw[s[i]].end_time = s[i] + d[i]; } } ptr->n = ptr->mpw.size(); return ptr; } int CalculateMaxWorkshops(Available_Workshops * ptr){ int count = 0; map<int, Workshop>::iterator it ; int prs = -1; bool hp = 0; for(it = ptr->mpw.begin(); it != ptr->mpw.end(); ){ int cur_end = it->second.end_time; if(prs != -1 && hp && prs <= it->second.start_time){ ++count; prs = -1; hp = 0; } if(cur_end <= (++it)->second.start_time){ ++count; hp = 0; prs = -1; } else { if(prs == -1) prs = cur_end; else prs = min(prs, cur_end); prs = min(prs, it->second.end_time); hp = 1; } } return count+1; }
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 →
It took me some time to figure it out. But this eventually worked and passed all the test cases.