Merge Intervals
Given an array intervals representing a collection of intervals, where each interval is intervals[i] = [start, end], write a function to merge all overlapping intervals and return an array of the non-overlapping intervals that cover all the intervals in the input.
Example:
Input: intervals = [[1,3],[2,6],[8,10]]
Output: [[1,6],[8,10]]
Explanation: The intervals [1,3] and [2,6] overlap, so they are merged into [1,6].
Input: intervals = [[1,4],[4,5]]
Output: [[1,5]]
This is the classic interval merging problem. The standard solution is to sort intervals by their start value, then scan them once while maintaining the last merged interval. If the next interval overlaps, extend the right boundary; otherwise, append the current interval to the result and start a new one. The main idea is greedy merging after sorting, with time complexity O(n log n) and linear scanning afterward.