MCPcopy Create free account
hub / github.com/codedecks-in/LeetCode-Solutions / merge

Method merge

Python/56_MergeIntervals.py:21–29  ·  view source on GitHub ↗
(self, intervals: List[List[int]])

Source from the content-addressed store, hash-verified

19#Space Complexity: O(n)
20class Solution:
21 def merge(self, intervals: List[List[int]]) -> List[List[int]]:
22 intervals.sort(key=lambda x: x[0]) #sort the intervals with starting time as the key
23 merged = []
24 for interval in intervals:
25 if not merged or merged[-1][1] < interval[0]: #If merged list is empty (we need to push the first interval) or Overlapping condn doesnt satisfy
26 merged.append(interval) #push the interval in merged array
27 else: #else
28 merged[-1][1] = max(merged[-1][1], interval[1]) #update the end time of last interval in merged array as maximum of end time of both intervals
29 return merged

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected