Insert Interval - Leetcode 57 - Python

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

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

  • @the_derpler
    @the_derpler 2 года назад +163

    This one kills me, I always get soooo close yet end up with a variety of edge cases. Thanks for your video.

    • @Oliver-Zen
      @Oliver-Zen 5 месяцев назад +2

      exactly same for me, this question has my most failed submission so far

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

      @@Oliver-Zen crazy edge cases if not thought through.

  • @PippyPappyPatterson
    @PippyPappyPatterson 2 года назад +66

    I come to your solutions first for new Leetcode problems. Sometimes I think, "wow there's got to be a more elegant way to do this." Then I look at the 🐍 solutions on Leetcode discussions and I realize how yours blows everything else out of the water.
    The best part of following along with your solutions is I find myself thinking through algorithms similar to you. It's like being in the lectures of a great math professor and assimilating their thought processes into my repertoire.

    • @roywastaken
      @roywastaken 2 года назад +15

      seriously... a lot of the LC discussion Python solutions are these dumb one-liners that miss the point about writing readable code in interviews
      Neetcode is top notch

    • @PippyPappyPatterson
      @PippyPappyPatterson 2 года назад +3

      @@roywastaken I've been working with some people that do more declarative programming via the `itertools` module (e.g. `zip`, `map`, `filter`, unpack operator `*`).
      I _do_ totally agree that the one-liners are impossible to read for anyone beside the author.
      _BUT_ , after I sat down and took the time to understand iterables, their iterators, and the basic `itertools`, implementing some idioms in single lines can compress the mental overhead of some leetcode solutions as I study them- even if I would expand them back out in an interview setting for legibility.

  • @anthonyuccello1458
    @anthonyuccello1458 2 года назад +287

    At the start of my interviews, Im definitely going to use the line "Lets right some neetcode today"

  • @hwang1607
    @hwang1607 7 месяцев назад +11

    pretty smart solution, i feel like its difficult to come up with in an interview, heres the commented code if it helps anyone:
    class Solution:
    def insert(self, intervals: List[List[int]], newInterval: List[int]) -> List[List[int]]:
    res = []

    for i in range(len(intervals)):
    if newInterval[1] < intervals[i][0]: # Right bound of of new interval is to the left of current interval
    res.append(newInterval)
    return res + intervals[i:]
    elif newInterval[0] > intervals[i][1]: # Left bound of new interval is to the right of current interval
    res.append(intervals[i])
    else: # Some part of the the new interval is overlapping with the current interval
    newInterval = [min(newInterval[0], intervals[i][0]), max(newInterval[1], intervals[i][1])]
    res.append(newInterval)
    return res

  • @symbol767
    @symbol767 2 года назад +30

    Your solution is so compact and clean, mine had 2 separate while loops, then a for loop to get the answer, this is way cleaner and better. This also opened my mind to how to think about this problem more logically.
    Thank you! Liked!

  • @GokulKrishna-r9l
    @GokulKrishna-r9l Год назад +1

    You could do this and avoid returning twice
    res = []
    i, n = 0, len(intervals)
    while i < n:
    if newInterval[1] < intervals[i][0]:
    break
    elif newInterval[0] > intervals[i][1]:
    res.append(intervals[i])
    else:
    newInterval = [min(newInterval[0],intervals[i][0]),max(newInterval[1],intervals[i][1])]
    i+=1
    res.append(newInterval)
    return res + intervals[i:]

  • @cw5948
    @cw5948 7 месяцев назад +17

    Deceptively tricky. Lots of edge cases to consider.

  • @licokr
    @licokr 8 месяцев назад +2

    now it is a must after solving problems, watching your explanation. this is absoultely amazing. i didn't think of expanding the new interval array so i had to make a lot of cases and plus, conditions you wrote look amazingly easy. knowing well what exactly you have to do. thanks a lot. i learned a lot today too

  • @edwardsolomon643
    @edwardsolomon643 8 месяцев назад +2

    Just can't emphasize enough how good you explain a problem!

  • @JJ-qv8co
    @JJ-qv8co 3 года назад +28

    Dude you upload faster than I can solve each day

  • @electric336
    @electric336 2 года назад +2

    Clever solution using the for loop to basically ignore all the intervals that are in between newInterval.

  • @BobbyMully
    @BobbyMully 2 года назад +3

    I did line sweep algorithm that was rather messy, but this way was much more elegant and efficient. An especially great solution for this one!

  • @emmanuelromero2258
    @emmanuelromero2258 Год назад

    Stack Solution
    Different Perspective
    class Solution:
    def insert(self, intervals: List[List[int]], newInterval: List[int]) -> List[List[int]]:
    # Time: O(n)
    # Space: O(n)
    res = [newInterval]
    for n in intervals:
    start, end = n
    iStart, iEnd = res.pop()
    # Compressing from Left Side
    if start = iStart :
    res.append([start, max(iEnd, end)])
    # Compressing from Right Side
    elif iStart = start :
    res.append([iStart, max(iEnd, end)])
    #Inserting in Ascending Order with No Compression
    elif start < iStart:
    res.append([start, end])
    res.append([iStart, iEnd])
    else:
    res.append([iStart, iEnd])
    res.append([start, end])
    return res

  • @karthiklfhs
    @karthiklfhs Год назад +4

    Shouldn't our insert routine modify the original intervals array in place?

  • @炒粿条-b1d
    @炒粿条-b1d 2 года назад +6

    This code is compact but a bit hard to understand. If the multi-return can be reduced to a single return, then it will look more intuitive

    • @abdul.arif2000
      @abdul.arif2000 Год назад +2

      from typing import List
      class Solution:
      def insert(
      self, intervals: List[List[int]], newInterval: List[int]
      ) -> List[List[int]]:
      res = []
      added = False
      for i in range(len(intervals)):
      if newInterval[1] < intervals[i][0]:
      res.append(newInterval)
      res += intervals[i:]
      added = True
      break
      elif newInterval[0] > intervals[i][1]:
      res.append(intervals[i])
      else:
      newInterval = [
      min(newInterval[0], intervals[i][0]),
      max(newInterval[1], intervals[i][1]),
      ]
      if not added:
      res.append(newInterval)
      return res

  • @KCODivyanshuKatyan
    @KCODivyanshuKatyan 2 года назад +1

    one of the most clearest explaination

  • @DavidDLee
    @DavidDLee Год назад +2

    A more interesting question would be how to store these intervals such that insertion will take less than O(N), that we don't need to iterate.

  • @karthik829
    @karthik829 3 года назад +6

    Preparing with your videos is really fun! Thanks!

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

    Pure gold! Saved lots of time finding solution. Great explanation.

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

    Cleaner Solution from LC:
    class Solution:
    def insert(
    self, intervals: List[List[int]], new_interval: List[int]
    ) -> List[List[int]]:
    total_intervals = len(intervals)
    index = 0
    merged_intervals = []
    # Case 1: No overlapping before merging intervals
    while index < total_intervals and intervals[index][1] < new_interval[0]:
    merged_intervals.append(intervals[index])
    index += 1
    # Case 2: Overlapping and merging intervals
    while index < total_intervals and new_interval[1] >= intervals[index][0]:
    new_interval[0] = min(new_interval[0], intervals[index][0])
    new_interval[1] = max(new_interval[1], intervals[index][1])
    index += 1
    merged_intervals.append(new_interval)
    # Case 3: No overlapping after merging new_interval
    while index < total_intervals:
    merged_intervals.append(intervals[index])
    index += 1
    return merged_intervals

  • @sandippatel2518
    @sandippatel2518 Год назад +1

    Your explanation is very easy to understand and helpful. Thank you for such a nice video.

  • @hey.............
    @hey............. 2 года назад +1

    No doubt you are at Google. Really clean and simple code

  • @biswajeetpadhi100
    @biswajeetpadhi100 Год назад

    ascending order = [ [ 1, 2 ] , [ 3 , 4 ] ]
    non- decreasing order = [ [ 1, 2 ] , [2, 3] ]

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

    I think understanding the cycle update and doing it with an outer loop is something that people would more likely come up in the interview! I wonder what is the better way to solve this in actual interview

  • @OnceUponaTimeSejal
    @OnceUponaTimeSejal 11 месяцев назад +1

    This is need to be done in-place, since they have asked us to return 'interval'.

  • @wigglesort
    @wigglesort 2 года назад +1

    You explained the logic and code very easily. I code in C++ but easily understood python solution

    • @muhammadtaimoor876
      @muhammadtaimoor876 Год назад

      the C++ solution for this one is a nightmare imo

    • @wigglesort
      @wigglesort Год назад

      @@muhammadtaimoor876 just a bit of coding practice

  • @drac4195
    @drac4195 Год назад

    Wow amazing way to code was stuck in this for long time best way to approach this problem!
    Here is my java code:
    class Solution {
    public int[][] insert(int[][] intervals, int[] newInterval) {
    int newS=newInterval[0];
    int newE=newInterval[1];
    ArrayList arr=new ArrayList();
    int i=0;
    for(i=0;i

  • @Pelosarri3
    @Pelosarri3 2 года назад +13

    Thoughts on using binary search to find the insertion point? considering that theyre sorted, would improve on time complexity

    • @Nora-pc6gy
      @Nora-pc6gy Год назад +7

      I was thinking the same idea, then I noticed we don’t know at most how many intervals we need to merge with the newinterval, so it’s still O(n) time complexity

    • @mucle6
      @mucle6 Год назад

      Thanks for this explanation!

    • @cantellmenow
      @cantellmenow Год назад +2

      @@Nora-pc6gy Do a second binary search.

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

      @@cantellmenow No, that will still lead to O(n) complexity, here's the start of the binary search solution:
      # O(log n)
      start = bisect_left(intervals, newInterval[0], key=lambda x: x[0])
      # insight: since intervals are non-overlapping, that means they're sorted by end too
      # so find where newInterval end fits using binary search again
      # O(log n)
      end = bisect_left(intervals, newInterval[1], lo=start, key=lambda x: x[1])
      # if there is more than 1 interval in between, delete intermediary ones and set edges
      # O(n) = 2:
      for _ in range(start+1, end):
      del intervals[start+1]

    • @wonderstruck.
      @wonderstruck. 2 месяца назад +2

      Both the start points and the end points are already sorted, so you can run two binary searches to find the merge positions. The problem is that building the output array is O(n) anyway, so it doesn't save you any time complexity.

  • @PanchalHappy
    @PanchalHappy 8 месяцев назад +2

    This one was so intuitive.

  • @brucewayne7875
    @brucewayne7875 3 года назад +4

    Another way to do this problem if you've already solved merge interval, is to just add the new interval to the sorted list, sort again and then apply the same merge interval algorithm.

    • @JackWutang
      @JackWutang 2 года назад +2

      If you did this, you'd be tacking on O(n log n) time for sorting and O(n) space for sorting, unnecessarily. Yes, this is another way to do the problem. However, it worsens efficiency and memory.

    • @MrJoeCoolBoy
      @MrJoeCoolBoy 2 года назад +6

      @@JackWutang Since the given intervals were already sorted, we can simply find the inserting index using the start time, insert it and do merge interval.

  • @tanvilakkad3860
    @tanvilakkad3860 Год назад

    Java code:
    public int[][] insert(int[][] intervals, int[] newInterval) {
    int n = intervals.length;
    LinkedList l = new LinkedList();
    for(int i=0; i

  • @parthcodes1374
    @parthcodes1374 9 дней назад

    I have a question in these interval question,
    You sometime used for loop with
    for i in range(len(intervals)):
    and sometimes with
    for start, end in intervals[1:]:
    How do we decide when to use which style would help better in any unknown problem?
    Thanks in advance if someone is trying to answer this

  • @VarunMittal-viralmutant
    @VarunMittal-viralmutant Месяц назад

    An alternate solution, O(N):
    def insert(intervals: List[List[int]], newInterval: List[int]) -> List[List[int]]:
    # Append new interval to current list in sorted order
    bisect.insort_left(intervals, newInterval)
    updated_intervals = []
    # start and end of first interval are markers, loop starts 2nd interval onwards
    prev_start = intervals[0][0]
    prev_end = intervals[0][1]
    for s, e in intervals[1:]:
    if s

  • @helario1
    @helario1 2 года назад +13

    curious to know what software and hardware do you use to draw and explain this problem beautifully? I can use it for my own practice while solving the problems.

    • @Kyle-ho4lj
      @Kyle-ho4lj 2 года назад +2

      he uses Paint 3D (comes with Windows). also OneNote Microsoft is good

  • @sauravchandra10
    @sauravchandra10 8 месяцев назад +1

    Such an elegant code!

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

    such elegant solution, thank you!

  • @jayberry6557
    @jayberry6557 2 года назад

    I think the good habit would be writing small comments throughout the code to memo the logic in more easy to decifer when u have any issue

  • @shilashm5691
    @shilashm5691 Год назад +1

    You can solve this problem just using the idea of merge intervals, try to think about that way, it is easier and faster solution

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

    at 5:05 you made a smiley face with the right circle/point

  • @anantsharma13
    @anantsharma13 5 месяцев назад

    When I tried it, it was a lot messy, missing multiple edge cases, this clean approach looks so good. Just wondering how much practice does it take to think of approaches like this ourself.

  • @deep.space.12
    @deep.space.12 2 года назад +3

    Since the inputs are sorted, couldn't you make it O(log(n)) by identifying the positions for insertion/merge with a binary search?

    • @cx123456
      @cx123456 Год назад

      yes

    • @MVandoff
      @MVandoff Год назад

      Yes but the insertion and merging will still be O(n).

    • @cantellmenow
      @cantellmenow Год назад

      @@MVandoff You can do another binary search using the end of inserted interval.

  • @nikhil199029
    @nikhil199029 8 месяцев назад +1

    I think we can use binary search to reduce time complexity here

  • @linlinpan3150
    @linlinpan3150 2 года назад +2

    this is just merged intervals. just add the newinterval into the intervals array and run merged intervals, no need to do the extra checking

  • @BGao3
    @BGao3 2 года назад +1

    This a great solution! Simple, elegant, and efficient.

  • @stephenchyau6486
    @stephenchyau6486 3 месяца назад

    great video. wondering if there's a follow up with the binary search version? I feel like there are more edge cases to consider there. sidenote: although true, worst time is not improved by binary solution, it will likely speed up the process if there are many items in intervals and newInterval has a large range.

  • @gagansaigaming598
    @gagansaigaming598 3 года назад +12

    We weren't able to see the leetcode runtime of the solution at the end after you submitted because you didn't zoom out enough. If you could make sure to do that in future videos, that would be awesome. Great video though

    • @johnc7436
      @johnc7436 2 года назад +27

      That doesn't matter to be honest. The time complexity/space complexity are the most important parts. If you submit the same solution 3 times on leetcode you'll get 3 different leetcode runtimes. If you don't believe me, try it.

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

    Oh my, this is so elegant!

  • @MrSerozka
    @MrSerozka 2 года назад +2

    Thanks for great content! Could someone explain, I can't understand: your code should append new interval to the end all the time, even if they overlapped in the middle, so result is not sorted anymore, but when I see results - they are sorted. How?

    • @mangalegends
      @mangalegends 11 месяцев назад

      If there's overlap in the middle, the loop will terminate in the first if clause on the next loop iteration (as long as newInterval doesn't overlap any other interval)

  • @AmolGautam
    @AmolGautam 10 месяцев назад +1

    Thank you for this.

  • @sinnyman2157
    @sinnyman2157 3 года назад +2

    Hey Neet, could u make a video about ' Number of Closed Islands' ?

  • @stith_pragya
    @stith_pragya Год назад

    Thank You So Much for this wonderful video...........🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻

  • @48_subhambanerjee22
    @48_subhambanerjee22 4 месяца назад +1

    Lets write some more neetcode today

  • @dgimop
    @dgimop 11 месяцев назад

    Aren't you supposed to return the original array 'intervals' after 'inserting' newInterval into intervals and return the original array 'intervals'? Using auxillary O(N) space makes the problem trivial to solve.

  • @Darkmatterkun
    @Darkmatterkun Год назад +1

    Doesn't it append the newInterval to the end of the res? How is that working?

  • @srikantd6054
    @srikantd6054 2 года назад

    Very Good Explanation

  • @edwardteach2
    @edwardteach2 3 года назад +3

    U a God - This explanation is way better than the Leetcode's solution

  • @amrholo4445
    @amrholo4445 8 месяцев назад

    Awesome man, Thank you 💕💕

  • @vm1662
    @vm1662 3 месяца назад

    Great solution! Pretty neat :)
    Mine had lots of if-else :P

  • @ashsheth1482
    @ashsheth1482 2 года назад +1

    Great explanation, thanks!

  • @mritunjaypathak251
    @mritunjaypathak251 2 года назад +3

    Just wounding can this be solved in log(n) using binary search

  • @danielshields8425
    @danielshields8425 Год назад

    Wouldn't the space complexity be O(n^2) bc the result array is a List of Lists? Or does each list here only count as one element?

    • @ajith12312
      @ajith12312 Год назад +2

      Each list counts as one element since its length is constant ie. 2

  • @expansivegymnast1020
    @expansivegymnast1020 2 года назад +1

    This problem took me forever because I had no idea Wtf that question was asking.

  • @dthnick
    @dthnick Год назад

    you are a magician

  • @eugenevedensky6071
    @eugenevedensky6071 3 года назад +4

    How about O(log n) with a binary search to search where to insert the interval.

    • @brianboylen9111
      @brianboylen9111 3 года назад +2

      Worst case you have to add everything into one big interval which is O(n). Don't think there is any way to get O(log n)

    • @eugenevedensky6071
      @eugenevedensky6071 3 года назад

      @@brianboylen9111 yes indeed. Best case, Big Omega is log n, Big O remains n with the scenario you described but I suppose it’s worth being aware of the log n early termination condition.

    • @dorondavid4698
      @dorondavid4698 3 года назад +3

      I thought the same thing when I first read the problem because of keywords, "sorted array" "insert at"...I personally would do a binary search for the starting interval position, then another binary search for ending interval position. logn + logn = logn

    • @eugenevedensky6071
      @eugenevedensky6071 3 года назад +1

      @@dorondavid4698 Yeah that's legit but it's a bit of fussy solution to code up in like 15 minutes with edge cases. At least that's the conclusion I came to.

    • @dorondavid4698
      @dorondavid4698 3 года назад

      @@eugenevedensky6071 Its not too bad, but you can mention it to the interviewer and see what they say as well, and if they want you to code it

  • @lizzard2023
    @lizzard2023 Год назад

    You're the best !!!!

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

    Note that requirements of this problem has changed, and this answer is no longer correct. In the new question, you have to update the intervals array IN PLACE and you cannot create a new array to return it.

  • @dhaanaanjaay
    @dhaanaanjaay 8 месяцев назад

    You are legend!!!

  • @sherlyyan9663
    @sherlyyan9663 2 года назад +1

    use exactly the same code and gives error. index out of range

  • @George-nx8zu
    @George-nx8zu 2 года назад +2

    Hmm wouldn't this cause the interval to be inserted twice potentially?

  • @bhargav1811
    @bhargav1811 Год назад

    I am not able to understand this concept of interval !! Help me plz !! :😢

  • @RathodDharmin
    @RathodDharmin Год назад

    The main thing you missed here is the binary search.

  • @ShivangiSingh-wc3gk
    @ShivangiSingh-wc3gk 2 года назад +1

    Took me 3 hours to solve this :(

  • @jaishriharivishnu
    @jaishriharivishnu 5 месяцев назад

    butter smooth

  • @idabagusdiaz
    @idabagusdiaz Год назад

    clever trick!

  • @ladydimitrescu1155
    @ladydimitrescu1155 Год назад

    this problem needs a complex coding ability

  • @kartik563
    @kartik563 8 месяцев назад

    Can you please solve problem no 228

  • @nikhilgoyal007
    @nikhilgoyal007 Год назад

    how to find if overlapping or not @5:00

    • @Moch117
      @Moch117 Год назад

      Write a helper function that checks if they DON'T overlap. This is easier since there are 4 cases of overlap versus 2 of non overlap.
      Non overlap:
      1. A and B: a.start < b.start and a.end < b.start
      2,. B and A: b.start < a.start and b.end < a.start

  • @pesallway
    @pesallway 3 года назад +1

    Brother Which Book Is good or best for python data structures and algorithms for beginners

    • @DreamScapeSurferSoul
      @DreamScapeSurferSoul 3 года назад

      This will help you a lot.
      ruclips.net/video/s91gEKf92K4/видео.html&ab_channel=AppMillers

  • @algorithmo134
    @algorithmo134 3 года назад +1

    I missed your videos :D Hope you are doing well!

  • @shaanyahoo
    @shaanyahoo 3 года назад

    HI, do you have plan to start a new channel or add videos on interview design questions

  • @asokansk4943
    @asokansk4943 3 года назад +2

    r u alive. Got placed in faang?

  • @eyeamkd
    @eyeamkd 2 года назад

    elegant solution but was only 5% faster than the all the python submissions : /

  • @nilskoch990
    @nilskoch990 Год назад

    is there a O(log(n)) time and O(1) space solution using binary search and working on the input list directly?

  • @cloud-rl9cc
    @cloud-rl9cc 3 года назад

    Good explanation!!!

  • @dianav357
    @dianav357 3 года назад

    so glad for the playback speed option

    • @farazahmed7
      @farazahmed7 2 года назад

      yeah you must have watched slowing it down lmfao

  • @AyubKhan-et6ur
    @AyubKhan-et6ur Год назад +1

    why was this medium? it feels like i've seen a number of "easy" ones harder than this

  • @Денис-ж3ф5р
    @Денис-ж3ф5р 4 месяца назад

    How is it medium? It is easy as for me

  • @vatsalsharma5791
    @vatsalsharma5791 3 года назад

    Plz Can you make a video on “snakes and ladders”

  • @anoopjoy501
    @anoopjoy501 2 года назад

    Why is the space complexity O(N)?! The output array space is not counted as auxiliary space right? Then we are not using any extra space.

    • @xnullxnullx
      @xnullxnullx 2 года назад +1

      Think of space complexity as total space with respect to input size (in our case with input size of n, you need O(n) bytes to represent n elements in the return list).

    • @farazahmed7
      @farazahmed7 2 года назад

      right the space complexity is O(1)

  • @mattclagett778
    @mattclagett778 Год назад

    Spent a good 4 hours banging my head against this one

    • @Moch117
      @Moch117 Год назад

      This is probably the hardest interval question

  • @omyx7413
    @omyx7413 2 года назад +5

    amongus

  • @mukeshkanna8116
    @mukeshkanna8116 3 года назад

    Are u alive? Y no videos mate.

  • @ahmednaeem4866
    @ahmednaeem4866 2 года назад

    Does anyone know similar questions to this on leetcode?

  • @themagickalmagickman
    @themagickalmagickman Год назад

    I did the problem 'merge intervals' first so I kinda cheated on this one. I inserted the new interval into intervals, sorted intervals, and then reimplemented merge intervals. Beat 98% funnily enough.
    intervals.append(newInterval)
    intervals.sort()
    -- Reimplement merge intervals exactly --
    done

    • @KishanTrivedi-ex5cg
      @KishanTrivedi-ex5cg Год назад

      Time complexity would be O(nlogn) because of sort()

    • @EduarteBDO
      @EduarteBDO 8 месяцев назад

      you don't really need to sort. just check if current interval comes before the last interval in res. If so insert it before the last

  • @CoDestination
    @CoDestination 10 месяцев назад

    java code + explanation
    ruclips.net/video/g_1Hp2zJSW4/видео.htmlsi=WUxvZHbhzOKRzqzB