Insert Interval - Leetcode 57 - Python

preview_player
Показать описание


0:00 - Read the problem
2:17 - Drawing Explanation
7:31 - Coding Explanation

leetcode 57

#sorted #array #python
Disclosure: Some of the links above may be affiliate links, from which I may earn a small commission.
Рекомендации по теме
Комментарии
Автор

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

the_derpler
Автор

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

anthonyuccello
Автор

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.

PippyPappyPatterson
Автор

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!

symbol
Автор

Dude you upload faster than I can solve each day

JJ-qvco
Автор

Preparing with your videos is really fun! Thanks!

karthik
Автор

Deceptively tricky. Lots of edge cases to consider.

cw
Автор

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

licokr
Автор

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

electric
Автор

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

sandippatel
Автор

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

mrfli
Автор

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

edwardsolomon
Автор

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

hwang
Автор

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.

DavidDLee
Автор

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

hey.............
Автор

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!

BobbyMully
Автор

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

wigglesort
Автор

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

davidoh
Автор

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

sinnyman
Автор

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:]

fpnwrvd