by
210 12
0
6
2,191
0
Top 1% !
Popular
Famous
Tagged
Specified
OpenSource
Popularity: 8705th place
LanguagePython
LicenseMIT_X11

[leetcode] Insert Interval

Copy Embed Code
<iframe id="embedFrame" style="width:600px; height:300px;"
src="https://www.snip2code.com/Embed/52754/-leetcode--Insert-Interval?startLine=0"></iframe>
Click on the embed code to copy it into your clipboard Width Height
Leave empty to retrieve all the content Start End
class Interval: def __init__(self, s=0, e=0): self.start = s self.end = e class Solution: def insert(self, intervals, newIntervals): """ Inserts the new interval into the list and returns merged intervals. 1. Appends the new interval to the list. 2. Sorts the list. 3. Iterates through and merges the new interval list. """ if not intervals: return [newInterval] intervals.append(newInterval) intervals.sort(lambda x, y: x.start - y.start) result = [] length = len(intervals) last = intervals[0] for i in range(1, length): current = intervals[i] if current.start <= last.end: # Merge intercetions last.end = max(last.end, current.end) else: result.append(last) last = current result.append(last) return result
If you want to be updated about similar snippets, Sign in and follow our Channels

blog comments powered by Disqus