Attending Workshops

  • + 0 comments

    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;

    ws_avail->n = n;
    
    for(int i=0; i < n; i++)
    {
        Workshop ws = {start_time[i], duration[i], start_time[i] + duration[i]};
        ws_avail->wsarr.push_back(ws);
    }
    
    return ws_avail;
    

    }

    int CalculateMaxWorkshops(Available_Workshops* ws_avail) { int cnt = 1; int cur; vector vec;

    sort(ws_avail->wsarr.begin(), ws_avail->wsarr.end(), [](const Workshop &w1, const Workshop &w2){return w1.et < w2.et;});
    
    cur = ws_avail->wsarr[0].et;
    
    for(int j=0; j<(ws_avail->n - 1); j++)
    {
        if(cur <=  ws_avail->wsarr[j+1].st)
        {
            cnt++;
            cur = ws_avail->wsarr[j+1].et;  
        }
    }
    
    return cnt;
    

    }

    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];

    for(int i=0; i < n; i++){
        cin >> start_time[i];
    }
    for(int i = 0; i < n; i++){
        cin >> duration[i];
    }
    
    Available_Workshops * ptr;
    ptr = initialize(start_time,duration, n);
    cout << CalculateMaxWorkshops(ptr) << endl;
    return 0;
    

    }