CODE-0005 · gemini-3.5-flash (default)
def merge_intervals(intervals):
    if not intervals:
        return []

    # Sort a deep copy of the intervals to prevent mutating the caller's input
    sorted_intervals = sorted([list(i) for i in intervals], key=lambda x: x[0])

    merged = [sorted_intervals[0]]
    for current in sorted_intervals[1:]:
        last = merged[-1]
        # If the current interval overlaps or touches the last merged interval
        if current[0] <= last[1]:
            last[1] = max(last[1], current[1])
        else:
            merged.append(current)

    return merged