INSERT INTERVAL | LEETCODE 57 | PYTHON SOLUTION

Sdílet
Vložit
  • čas přidán 24. 07. 2024
  • Channel Discord Community: / discord
    Problem Link: leetcode.com/problems/insert-...
    Today we are solving another interval based problem that combines some of the logic from the other ones: Insert Interval (LC # 57).
    TIMESTAMPS:
    00:00 Intro
    00:08 Question Prompt
    00:49 Examples
    04:12 Solution Intuition
    05:50 Coding
    14:10 Time/Space Complexity
    15:45 Outro
  • Věda a technologie

Komentáře • 6

  • @dnm9931
    @dnm9931 Před 2 měsíci

    Tricky one to explain! I appreciate your efforts as always

  • @greatestever2914
    @greatestever2914 Před 5 měsíci

    if (!intervals.size()) return {newInterval};
    int i = 0, n = intervals.size();
    vector ans;
    // condition 1 - continue to merge as long as there is no overlap
    while (i < n && intervals[i][1] < newInterval[0]) {
    ans.push_back(intervals[i]);
    i++;
    }
    // condition 2 - overlap conflict - resolve by merging
    while (i < n && intervals[i][0]

    • @MadWe582
      @MadWe582 Před 3 měsíci

      need to push the newInterval to ans before the last while loop

    • @greatestever2914
      @greatestever2914 Před 3 měsíci

      @@MadWe582 ahh yes you are correct.. this should be done after the 2nd while loop to insert the newly merged intervals.
      I will not fix it in, but let whoever is reading this solution figure it out where to insert it :)

  • @user-nj1xh8ot3f
    @user-nj1xh8ot3f Před 4 měsíci

    This ignores the requirement that you have to modify the intervals array in place and return it. You're not supposed to create a new array with the new intervals. I'm not saying that it won't pass, but it technically doesn't meet the requirements the problem is asking.

    • @LastVoyage
      @LastVoyage Před 13 dny

      It literally says:
      "Note that you don't need to modify intervals in-place. You can make a new array and return it."