CODE-0005 · kimi-k2.7-code (default)def merge_intervals(intervals):
if not intervals:
return []
# Create a copy to avoid mutating the input
sorted_intervals = sorted(intervals, key=lambda x: x[0])
merged = [list(sorted_intervals[0])]
for current in sorted_intervals[1:]:
last = merged[-1]
# Check if current overlaps or touches the last merged interval
if current[0] <= last[1]:
# Merge them: extend the end if necessary
last[1] = max(last[1], current[1])
else:
# No overlap, add as new interval
merged.append(list(current))
return merged