L9. Insert Intervals | Greedy Algorithms Playlist

Поделиться
HTML-код
  • Опубликовано: 25 май 2024
  • Find problem link, notes under Step 12: takeuforward.org/strivers-a2z...
    Follow me on socials: linktr.ee/takeUforward

Комментарии • 27

  • @sohamsonwane1534
    @sohamsonwane1534 Месяц назад +16

    while(i

  • @KartikeyTT
    @KartikeyTT 18 дней назад +3

    CORRECTION : In the 2nd while loop initialization it will be while(i < n && intervals[i][0]

  • @KeigoEdits
    @KeigoEdits 9 дней назад +1

    when I saw this question, there was in my mind that it is saying to insert, ofcourse it will be inplace and end up doing inplace which beats 5% people in runtime😂 BUT beats 98% people in terms of Memory
    Here is the code, if anyone wanna see this approach
    class Solution {
    public:
    vector insert(vector& inter, vector& newinter) {
    int n=inter.size(),low=0,high=n-1,ans=-11;
    while(low

  • @samratbhattacharya593
    @samratbhattacharya593 Месяц назад +5

    small recomendations in the code:
    1. use different variables to find the starting and ending ranges for intersection values
    2. use a = in the 2nd while to take in the range when the newInterval[1] matches the intervals[i][1] in any case, for eg: newInterval={4,8} and the one of the intervals is [.......{8,10},.....]
    code:
    vector insert(vector& intervals, vector& newInterval) {
    vectorans;
    int i=0;
    while(i

    • @anoopkumaryadav5084
      @anoopkumaryadav5084 Месяц назад

      It would give wrong output. Inside the second while loop condition should be i

  • @sohamsonwane1534
    @sohamsonwane1534 Месяц назад +4

    for java
    class Solution {
    public int[][] insert(int[][] intervals, int[] newInterval) {
    ArrayList ans = new ArrayList();
    int start= 0;
    int end= 1;
    int n = intervals.length;
    int i = 0;
    //left part which do not have overlap
    while(i

    • @jayaramallamsetti2219
      @jayaramallamsetti2219 День назад

      Here is the smaller code:
      class Solution {
      public int[][] insert(int[][] intervals, int[] newInterval) {
      int n = intervals.length;
      List ans = new LinkedList();
      int i =0;
      while(i

  • @purushottam108
    @purushottam108 16 дней назад

    class Solution {
    public:
    vector insert(vector& intervals, vector& newInterval) {
    int n = intervals.size();
    int i = 0;
    vectorresult;
    while(i

  • @psinity
    @psinity Месяц назад

    CPP
    class Solution {
    public:
    vector insertNewEvent(int n, vector &intervals, vector &newEvent)
    {
    vector ans;
    int i = 0;
    while(i < n && intervals[i][1] < newEvent[0])
    {
    ans.push_back(intervals[i]);
    i++;
    }
    // int start = -1
    while(i < n && intervals[i][0] newEvent[1])
    {
    ans.push_back(intervals[i]);
    i++;
    }
    return ans;
    }
    };

  • @silent-st1no
    @silent-st1no Месяц назад +1

    super simple code
    class Solution {
    public:
    vector insert(vector& intervals, vector& newInterval) {
    intervals.push_back(newInterval);
    sort(intervals.begin(),intervals.end());
    int n=intervals.size();
    vectorans;
    for(int i=0;ians.back()[1])
    ans.push_back(intervals[i]);
    else
    ans.back()[1]=max(ans.back()[1],intervals[i][1]);
    }
    return ans;
    }
    };

  • @Dsa_kabaap
    @Dsa_kabaap Месяц назад +1

    Sir please start making videos on strings and stacks

  • @KartikeyTT
    @KartikeyTT 18 дней назад

    tysm sir

  • @Shivi32590
    @Shivi32590 День назад

    thank you

  • @nikhilmadaan29
    @nikhilmadaan29 29 дней назад +1

    in the last question (1,2) & (2,3) were not considered as overlapping but in this one it will considered, how can we be sure ?

  • @RachitKala-cp4uh
    @RachitKala-cp4uh 45 минут назад

    Understood

  • @pranavmisra5870
    @pranavmisra5870 24 дня назад

    nice

  • @yashmundada2483
    @yashmundada2483 8 дней назад

    This could have been done with binary search as well.
    Thats the point of this question otherwise what's the difference in this question and merge intervals.
    Could have just added the new interval into the old and applied merge intervals ?

  • @shikhartomar3329
    @shikhartomar3329 Месяц назад

    phenomenal playlist

  • @user-id4ct8jv9v
    @user-id4ct8jv9v 14 дней назад

    watched

  • @HarishchandraKumar-ew4oh
    @HarishchandraKumar-ew4oh Месяц назад

    very well explained playlist

  • @silent-st1no
    @silent-st1no Месяц назад

    super simple
    class Solution {
    public:
    vector insert(vector& intervals, vector& newInterval) {
    intervals.push_back(newInterval);
    sort(intervals.begin(),intervals.end());
    int n=intervals.size();
    vectorans;
    for(int i=0;ians.back()[1])
    ans.push_back(intervals[i]);
    else
    ans.back()[1]=max(ans.back()[1],intervals[i][1]);
    }
    return ans;
    }
    };

    • @anubhav0355
      @anubhav0355 Месяц назад +1

      given intervals are already sorted, use it rather than sorting it again and converting this question into merge intervals!

    • @kashvibhanderi771
      @kashvibhanderi771 Месяц назад

      ​​@@anubhav0355Look carefully this is converted into merge interval question

    • @psinity
      @psinity Месяц назад

      this is an nlogn approach, will give TLE, we need O(n). And yeah as it is already sorted we shouldn't sort again

    • @silent-st1no
      @silent-st1no Месяц назад

      @@psinity However it is accepted

    • @dabhijay4997
      @dabhijay4997 16 дней назад

      May be you can identify where to insert newInterval into given intervals using binary search after that you can use merge interval problem solution.(No need to sort in merge interval problem solution)
      Time complexity:O(logn) + O(n).