Attending Workshops

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

    }