Sort by

recency

|

36 Discussions

|

  • + 0 comments

    include

    include

    include

    using namespace std;

    struct Activity { int start; int finish; int index; // Original index of the activity };

    bool compare(Activity a, Activity b) { return a.finish < b.finish; }

    void maxActivities(vector& activities) { int n = activities.size(); // Sort activities based on finish time sort(activities.begin(), activities.end(), compare);

    vector<int> selectedActivities;
    selectedActivities.push_back(activities[0].index);
    int prevFinish = activities[0].finish;
    
    for (int i = 1; i < n; ++i) {
        if (activities[i].start >= prevFinish) {
            selectedActivities.push_back(activities[i].index);
            prevFinish = activities[i].finish;
        }
    }
    
    // Output
    for (int i = 0; i < selectedActivities.size(); ++i) {
        cout << selectedActivities[i] << "\t";
    }
    cout << endl;
    

    }

    int main() { int n; cin >> n;

    vector<Activity> activities(n);
    for (int i = 0; i < n; ++i) {
        cin >> activities[i].start >> activities[i].finish;
        activities[i].index = i + 1; // Assign original index
    }
    
    maxActivities(activities);
    
    return 0;
    

    }****

  • + 1 comment

    My typescript solution:

    let maxCount = 0;
        let lastTwo = [[0,0],[0,0]];
        intervals.sort((a,b) => a[1]-b[1]);
        intervals.forEach((interval) => {
            if(interval[0] > lastTwo[1][1]) {
                maxCount++;
                lastTwo[1] = interval;
            } else if (interval[0] > lastTwo[0][1]) {
                maxCount++;
                lastTwo[0] = interval
                lastTwo.sort((a,b) => a[1]-b[1])
            }
        })
        return maxCount;
    
  • + 0 comments

    Here is my solution in java, javascript, python, C, C++, Csharp HackerRank Interval Selection Problem Solution

  • + 0 comments

    Here is Interval Selection problem solution - https://programs.programmingoneonone.com/2021/07/hackerrank-interval-selection-problem-solution.html

  • + 1 comment

    I understand the question after a good 15 minutes of reading. Not Google interview worthy problem solving skills yet, but I'm proud of myself for getting this far.