Leetcode 1353. Maximum Number of Events That Can Be Attended

Sdílet
Vložit
  • čas přidán 27. 07. 2024
  • This is a detailed solution video to the question "Maximum Number of Events That Can Be Attended".
    leetcode.com/problems/maximum...

Komentáře • 42

  • @princemehta1655
    @princemehta1655 Před 4 lety +4

    your code when run at leetcode is giving TLE so can you please help me to resolve

    • @mohammadfraz
      @mohammadfraz  Před 4 lety +7

      We are inserting number of days in the set.
      Just change it to the maximum day given in array instead of 10^5.
      It would solve the problem.😊

    • @sitanshushukla1820
      @sitanshushukla1820 Před 4 lety +3

      @@mohammadfraz it still gives tle for the last test case

    • @jigardoshi2852
      @jigardoshi2852 Před 4 lety +1

      @@sitanshushukla1820 Shouldnt you limit it to number of events? your answer can never exceed number of events

    • @kollivenkatamadhukar5059
      @kollivenkatamadhukar5059 Před 3 lety

      @@sitanshushukla1820 Beacuse u have to use segment tree

    • @kollivenkatamadhukar5059
      @kollivenkatamadhukar5059 Před 3 lety

      @@mohammadfraz The time complexity of your solution is not O(n log n)

  • @ManishSharma-fi2vr
    @ManishSharma-fi2vr Před 2 lety +6

    In comparator function pass both vectors by reference instead of pass by value. All test cases will be passed. And also do optimization as Fraz bhaiya said by inserting values 1 to maximum time in set instead of 1e5.

    • @ayush5234
      @ayush5234 Před rokem

      Dude thanks a lot. Passing by reference to the comparator function passed all test cases , earlier i was getting tle. Was stuck on this for several hours.THANK YOU SO MUCH
      ❤️❤️❤️❤️❤️❤️

  • @vk1618
    @vk1618 Před 4 lety +1

    Iterating by event (as it is the upper bound), event that is going to end next should be considered first at any given moment - hence the sorting, use of std::set as need to maintain order. Any time based max-min problem has sorting involved.

  • @mohdsaadalikhan7209
    @mohdsaadalikhan7209 Před 2 lety

    "We are inserting number of days in the set.
    Just change it to the maximum day given in array instead of 10^5."
    but why it gives tle on inserting 10^5 elements in set ...it is well within 10^8 operations (i.e. nlogn = 10^5*log(10^5) = 1.6*10^6)????

  • @pulkitranjan8189
    @pulkitranjan8189 Před rokem

    what is the logic behind sorting it by end day ??

  • @ojaswibhardwaj3960
    @ojaswibhardwaj3960 Před 3 lety

    Thank you so much !!!

  • @arpit35007
    @arpit35007 Před 4 lety

    Why sorting based on endTime and not the startTime, didn't understand this even in meeting room problem.

    • @mohammadfraz
      @mohammadfraz  Před 4 lety +3

      we are willing to choose the events which will get finished first ignoring the starting time.

    • @AnkurGupta-zu7jj
      @AnkurGupta-zu7jj Před 3 lety +2

      Try for this case: [[1,2],[1,2],[3,3],[1,5],[1,5]]. It will get ugly if we choose sorting based on startTime.

  • @iWontFakeIt
    @iWontFakeIt Před rokem +1

    Use this those who are getting TLE, to understand this better first study the SJF algorithm from OS and then try this problem, it will be breeze to solve it...Hope it helps
    class Solution {
    public:
    int maxEvents(vector& events) {
    // introduce a timeline variable --> which will go from day 0 to day n
    // this is done, because according to question an event spans through a number of days
    // and we can attend this event anyday in between, so we will basically attend one event
    // in one day, hence the timeline variable will tell us which event to attend on the particular
    // day, also for maximum events we will sort the events based on their starting times
    // and we will use the timeline variable to push those events into the set/pq which have
    // arrived till that time, and attend the one having lower end time first and so on...
    int n = events.size();
    sort(begin(events), end(events));
    int timeline = 0, index = 0;
    int count = 0;
    priority_queue minheap;
    while(index < n || !minheap.empty()){
    // edge case when large gap b/w events or start event
    if(minheap.empty() && timeline < events[index][0]){
    timeline = events[index][0];
    }
    // pick the events that have arrived till this timeline
    while(index < n && events[index][0]

  • @shiwang789
    @shiwang789 Před 2 lety

    the time complexity if this solution is O(nlogn + n*d). It is possible to do it in O(nlogn + d)

  • @ratnasanjay
    @ratnasanjay Před 2 lety

    Thankyou bhaiya for this video

  • @mayurjain7951
    @mayurjain7951 Před 3 lety

    Good solution but you shouldn't take set here! It will increase time.

  • @raveenakushwah8729
    @raveenakushwah8729 Před 3 lety

    Its giving time limit exceeded on leet code

  • @gorakhkumargupta491
    @gorakhkumargupta491 Před 2 lety

    Great explanation sir

  • @anonymous-kl1un
    @anonymous-kl1un Před 4 lety

    Please add more and more videos

  • @aryan__o
    @aryan__o Před rokem

    Thanks bro

  • @pranaykumar9433
    @pranaykumar9433 Před 2 lety +1

    job sequencing algorithm

  • @prakhyat2001
    @prakhyat2001 Před rokem

    tnx bud

  • @rishirajkalita9883
    @rishirajkalita9883 Před 3 lety +1

    this code will give tle

  • @DevanshAgarwal-tf7jt
    @DevanshAgarwal-tf7jt Před rokem

    C++ code:
    bool static comp (vector & a, vector & b) {
    if (a[1] < b[1]) return true; // sorting acc to end time
    else if (a[1] == b[1]) return a[0] < b[0]; // sorting acc to start time if same end time
    return false;
    }
    int maxEvents(vector& events) {
    sort (events.begin(), events.end(), comp);
    int count = 0;
    set days;
    for (int i = 1; i

  • @ShyToSassySaga
    @ShyToSassySaga Před rokem +4

    poor explanation

  • @Sauravk2107
    @Sauravk2107 Před rokem +2

    very poor explanation. No intution no thinking

  • @jatingupta5986
    @jatingupta5986 Před 4 lety +1

    slightly better java solution
    class Solution {
    public int maxEvents(int[][] events) {

    // sort the events array by the end date and if they are equal then by start date
    Arrays.sort(events, new Comparator() {
    public int compare(int[] a, int[] b) {
    if (a[1] > b[1]) {
    return 1;
    } else if (a[1] == b[1]) {
    if (a[0] > b[0]) {
    return 1;
    } else {
    return -1;
    }
    } else {
    return -1;
    }
    }
    });

    // initilization of vars
    int counter = 0;
    int start = 0;
    int end = 1;

    // find the max time and min time to fill the set
    int max = 0;
    int min = Integer.MAX_VALUE;
    for (int row = 0; row < events.length; row++) {
    max = max > events[row][end] ? max : events[row][end];
    min = min < events[row][start] ? min : events[row][start];
    }

    // create set and fill it with true values, that signifies the days of the event which have not been visited. If false then it is visited
    boolean[] set = new boolean[max-min+1];
    for (int i = min; i

  • @AshutoshKumar-gg2ox
    @AshutoshKumar-gg2ox Před 3 lety +6

    FOR THOSE GETTING TLE.
    upper_bound(Set.begin(), Set.end(), x) is a function that doesn't know anything about std::set internal structure.
    Set iterators are not random-access meaning that upper_bound has to perform O(n) iterator movements in order to access any position in the container.
    That's why you should never use lower_bound/upper_bound with std::set
    but you can use Set.lower_bound(x)/Set.upper_bound(x) that perform a search over a binary search tree, that works obviously in log n/
    So basically use : days.lower_bound(s)
    Reference: codeforces.com/blog/entry/22074

    • @jskpa4851
      @jskpa4851 Před rokem +3

      I was getting TLE using upper_bound(Set.begin(), Set.end(), x) and was wondering wtf is wrong. Surely got something super important to learn. Thanks mate!