So I'm doing a practice problem where I'm trying to merge elements from a 2d array and return non-overlapping intervals.
I came up with the solution:
def merge(self, intervals: List[List[int]]) -> List[List[int]]:
sorted(intervals, key=lambda x: x[0])
merged_intervals = []
print(intervals)
for interval in intervals:
if not merged_intervals:
merged_intervals.append(interval)
else:
if merged_intervals[-1][1] >= interval[0]:
merged_intervals[-1][1] = interval[1]
else:
merged_intervals.append(interval)
return merged_intervals
But I'm having an issue where if the test case is [[1,4],[0,4]]
sorted() is returning the list in the same order. Am I misunderstanding how the lambda is used in the sorting algorithm here?