INSERT INTERVAL | LEETCODE 57 | PYTHON SOLUTION

Поделиться
HTML-код
  • Опубликовано: 26 ноя 2024

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

  • @dnm9931
    @dnm9931 6 месяцев назад

    Tricky one to explain! I appreciate your efforts as always

  • @greatestever2914
    @greatestever2914 9 месяцев назад

    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 7 месяцев назад

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

    • @greatestever2914
      @greatestever2914 7 месяцев назад

      @@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 :)

  • @TaikiTanaka-n6c
    @TaikiTanaka-n6c 8 месяцев назад

    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 4 месяца назад +2

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