Attending Workshops

Sort by

recency

|

190 Discussions

|

  • + 1 comment

    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;
    

    }

  • + 0 comments
    struct Workshops{
        friend ostream &operator<<(ostream &os, const Workshops &obj);
        int start_time;
        int end_time;
        int duration;
        bool operator<(const Workshops &rhs){
        return (this->end_time < rhs.end_time);
        }
    };
    ostream &operator<<(ostream &os, const Workshops &obj){
        os << obj.start_time << ": " << obj.end_time << ": "
        << obj.duration << "\n";
        return os;
    }
    struct Available_Workshops{
        int n;
        vector<Workshops> vec;
    };
    Available_Workshops* initialize(int start_time[], int duration[], int num)
    {
        Available_Workshops *avail = new Available_Workshops;
        avail->n = num;
        Workshops test;
        for(int i{0}; i < num; i++){
            test.start_time = start_time[i];
            test.duration = duration[i];
            test.end_time = start_time[i] + duration[i];
            avail->vec.push_back(test);
        }
        sort(avail->vec.begin(), avail->vec.end());
        return avail;
    }
    int CalculateMaxWorkshops(Available_Workshops *test){
        int w_count = 1;
        int test_end_time = test->vec.at(0).end_time;
        for(int i{1}; i < test->n; i++){
            if(test_end_time <= test->vec.at(i).start_time){
                w_count++;
                test_end_time = test->vec.at(i).end_time;
            }
        }
        return w_count;
    }
    
  • + 0 comments

    //try this

    include

    include

    include

    using namespace std;

    class Workshop { public: int start_time; int end_time;

    Workshop(int start, int duration) {
        start_time = start;
        end_time = start + duration;
    }
    

    };

    class Available_Workshops { public: vector workshops;

    Available_Workshops(vector<Workshop> ws) {
        workshops = ws;
    }
    

    };

    Available_Workshops* initialize(int start_time[], int duration[], int n) { vector workshops; for (int i = 0; i < n; i++) { workshops.push_back(Workshop(start_time[i], duration[i])); } Available_Workshops* available_workshops = new Available_Workshops(workshops); return available_workshops; }

    int CalculateMaxWorkshops(Available_Workshops* ptr) { vector& workshops = ptr->workshops;

    sort(workshops.begin(), workshops.end(), [](const Workshop& w1, const Workshop& w2) {
        return w1.end_time < w2.end_time;
    });
    
    int max_workshops = 0;
    int last_end_time = 0;
    
    for (const Workshop& workshop : workshops) {
        if (workshop.start_time >= last_end_time) {
            max_workshops++;
            last_end_time = workshop.end_time;
        }
    }
    
    return max_workshops;
    

    }

    
    

    int main() { int n; // Number of workshops cin >> n;

    int start_time[n];
    int 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* ptr = initialize(start_time, duration, n);
    cout << CalculateMaxWorkshops(ptr) << endl;
    
    delete ptr;
    return 0;
    

    }