Attending Workshops

Sort by

recency

|

188 Discussions

|

  • + 0 comments

    Here is the work energy theorem

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

    }