Merge Intervals
Intervals and Scheduling DSA practice problem on Onlearn.
Difficulty: medium.
Topics: Merging Overlapping Intervals in an Array, Two Pointers, Sorting, Intervals, Merge Sort, Time Complexity, Space Complexity, Greedy Algorithm, Array Manipulation, complexity analysis, greedy algorithm, sorting algorithms, intervals, general programming.
Problem Statement Given an array of intervals where intervals[i] = [start i, end i], merge all overlapping intervals, and return an array of the non overlapping intervals that cover all the intervals in the input. Intervals are considered overlapping if the start time of one interval is less than or equal to the end time of the previous interval. The resulting intervals should be sorted in ascending order of their start times. Input Specification A 2D integer array (list of lists) representing the intervals. Each inner array contains exactly two integers: [start, end]. Output Specification A 2D integer array representing the merged non overlapping intervals. Constraints 1 ≤ intervals.length ≤ 10^4 intervals[i].length == 2 0 ≤ start i ≤ end i ≤ 10^4 The order of merged intervals in the output should be sorted by start time. Sample Test Cases Sample Input 1 Sample Output 1 Explanation: Since intervals [1, 3] and [2, 6] overlap, merge them into [1, 6]. Sample Input 2 Sample Output 2 Explanation: Intervals [1, 4] and [4, 5] are considered overlapping. Sample Input 3 Sample Output 3 Explanation: The intervals overlap and are merged into the total range covered, which is 0 to 4. Note that the input is not necessarily sorted. Difficulty Level Medium