Attending Workshops

Sort by

recency

|

192 Discussions

|

  • + 0 comments
    struct Workshop{
        int starttime = 0 , duration = 0, endtime = 0;
        
        Workshop(int _startime, int _duration){
            starttime = _startime;
            duration = _duration;
            endtime = _startime + _duration;
        }
        
    };
    
    struct Available_Workshops{
        int len = 0;
        vector<Workshop> workshops;
        
        Available_Workshops(int _len, vector<Workshop> _workshops){
            len=_len;
            workshops=_workshops;
        }
    
    };
    
    Available_Workshops* initialize(int* startime, int* duration, int n){
        vector<Workshop> workshops;
            
        for (int i = 0; i<n; i++) {
            workshops.emplace_back(startime[i], duration[i]);      
        }
            
        return new Available_Workshops(n, workshops);
    }
    
    int CalculateMaxWorkshops(Available_Workshops* ptr){
        int current_end = 0, scheduled = 0;
    
        vector<Workshop> workshops = ptr->workshops;
        
        sort(workshops.begin(), workshops.end(), [](const Workshop& a, const Workshop& b) {
            return a.endtime < b.endtime;
        });
        
        for (Workshop& w : workshops){
            if (w.starttime >= current_end) {
                scheduled++;
                current_end = w.endtime;
            }
        }
        
        return scheduled;
    }
    
  • + 0 comments

    Here is Attending workshops solution in c++ - https://programmingoneonone.com/hackerrank-attending-workshops-solution-in-cpp.html

  • + 2 comments

    I hope someone doesn't struggle like I did, questioning how in the sample input, workshops 0,1,3 and 5 got selected as the non-overlapping one's.

    The challenge editor made a problem and the correct sequence of non-overlapping workshops by start time should be 1, 3, 5 and 8.

    Other than that minor correction, everything else works seemlessly.

  • + 0 comments

    include

    include

    include

    using namespace std;

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

    struct Available_Workshops { int n;
    vector workshops; };

    Available_Workshops* initialize(int start_time[], int duration[], int n) { Available_Workshops* aw = new Available_Workshops(); aw->n = n; for (int i = 0; i < n; ++i) { Workshop ws; ws.start_time = start_time[i]; ws.duration = duration[i]; ws.end_time = start_time[i] + duration[i]; aw->workshops.push_back(ws); } return aw; }

    int CalculateMaxWorkshops(Available_Workshops* ptr) {

    vector<Workshop>& workshops = ptr->workshops;
    
    sort(workshops.begin(), workshops.end(), [](const Workshop& a, const Workshop& b) {
        return a.end_time < b.end_time;
    });
    
    int count = 0;          
    int last_end_time = 0;  
    for (const auto& ws : workshops) {
        if (ws.start_time >= last_end_time) {
            ++count;
            last_end_time = ws.end_time;
        }
    }
    
    return count;
    

    }

    my solution(cp20++): int main() { int n; cin >> n;

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

    }

  • + 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;
    

    }