Attending Workshops

Sort by

recency

|

188 Discussions

|

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

    }

  • + 0 comments

    I just tried a new solution in C++. I found that using a deque with workshops sorted by start time and duration is more convenient. First, store all workshops inside that deque, sort them, and then use a helper deque to get the maximum number of workshops with no overlap. The trick is to take the workshop with the earliest end time if they overlap. My code works and passed all tests successfully; however, it can be further improved.

    struct Workshop
    {
      time_t start_time, end_time;
      int duration;
      Workshop(int s_time, int dur) : start_time{s_time},
                                      duration{dur},
                                      end_time{s_time + dur} {}
    };
    bool compare_w(Workshop *w1, Workshop *w2)
    {
      if (w1->start_time == w2->start_time)
        return w1->duration < w2->duration;
      return w1->start_time < w2->start_time;
    }
    
    struct Available_Workshops
    {
      int n;
      deque<Workshop *> workshops;
      Available_Workshops(int i) : n{i} {}
    };
    
    Available_Workshops *initialize(int start_times[], int duration[], int n)
    {
      Available_Workshops *workshops = new Available_Workshops(n);
      for (int i = 0; i < n; ++i)
      {
        Workshop *w = new Workshop(start_times[i], duration[i]);
        workshops->workshops.push_back(move(w));
      }
      sort(workshops->workshops.begin(), workshops->workshops.end(), compare_w);
      return workshops;
    }
    
    int CalculateMaxWorkshops(Available_Workshops *ptr)
    {
      deque<Workshop *> ws;
      ws.push_front(ptr->workshops.front());
      ptr->workshops.pop_front();
      while (!ptr->workshops.empty())
      {
        auto cw = ptr->workshops.front();
        auto nw = ws.front();
    
        if (cw->start_time >= nw->end_time)
        {
          ws.push_front(cw);
          ptr->workshops.pop_front();
          continue;
        }
    
        if (cw->start_time == nw->start_time)
        {
          ptr->workshops.pop_front();
          continue;
        }
        if (cw->start_time <= nw->end_time && cw->end_time <= nw->end_time)
        {
          ws.pop_front();
          ws.push_front(cw);
          ptr->workshops.pop_front();
          continue;
        }
        ptr->workshops.pop_front();
      }
      int cnt = ws.size();
    
      for (Workshop *w : ws)
      {
        delete w;
      }
      delete ptr;
      return cnt;
    }
    
  • + 0 comments
    struct Workshop {
        int start_time;
        int duration;
        int end_time;
    };
    
    struct Available_Workshops {
        int n {0};
        Workshop* workshops;
        
        Available_Workshops(int n) : n(n) {
            workshops = new Workshop[n];
        }
        ~Available_Workshops() {
            delete[] workshops;
            workshops = nullptr;
        }
    };
    
    Available_Workshops* initialize(int *start_time, int *duration, int n) {
        Available_Workshops* obj = new Available_Workshops(n); 
        for (int i = 0; i < n; ++i) {
            obj->workshops[i].start_time = start_time[i];
            obj->workshops[i].duration = duration[i];
            obj->workshops[i].end_time = start_time[i] + duration[i];
        }
        return obj;
    }
    
    int CalculateMaxWorkshops(Available_Workshops *ptr) {
        if (ptr == nullptr || ptr->workshops == nullptr || ptr->n <= 0)
            return 0;
    
        // Sort workshops based on their END times
        std::sort(ptr->workshops, ptr->workshops + ptr->n, 
            [](const Workshop& a, const Workshop& b) {
                return a.end_time < b.end_time;
            }
        );
    
        int maxWorkshops {0}, endTime {0};
        for (int i {0}; i < ptr->n; ++i) {
            if (ptr->workshops[i].start_time >= endTime) {
                // Non-overlapping workshop found
                ++maxWorkshops;
                endTime = ptr->workshops[i].end_time;
            }
        }
        return maxWorkshops;
    };