===== ISTEM =====
Solve the following programming task.

TASK: Merge Intervals

Write a function 'merge_intervals(intervals)' that takes a list of closed intervals that may overlap (e.g. [[1,3],[2,6],[8,10],[15,18]]) and returns a new list with the overlapping ones merged.

STARTER CODE (python):
def merge_intervals(intervals: list[list[int]]) -> list[list[int]]:
    # Write your code here
    pass


RULES:
- Keep the function name and signature EXACTLY as given.
- Return working code only. No explanations.
- Put the code in a single ``` block.
- Try to solve it first. If you genuinely cannot, write only this single
  line instead of producing faulty code: CANNOT_SOLVE

===== HAM YANIT =====
```python
def merge_intervals(intervals: list[list[int]]) -> list[list[int]]:
    merged = []
    for start, end in sorted(intervals):
        if merged and start <= merged[-1][1]:
            merged[-1][1] = max(merged[-1][1], end)
        else:
            merged.append([start, end])
    return merged
```