-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMergeIntervals.cs
31 lines (31 loc) · 904 Bytes
/
MergeIntervals.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/*
Given an array of intervals where intervals[i] = [starti, endi], merge all overlapping intervals,
and return an array of the non-overlapping intervals that cover all the intervals in the input.
*/
public class Solution
{
public int[][] Merge(int[][] intervals)
{
if(intervals.Length == 0)
{
return intervals;
}
Array.Sort(intervals, (a, b) => a[0] - b[0]);
List<int[]> result = new List<int[]>();
int[] current = intervals[0];
for(int i = 1; i < intervals.Length; i++)
{
if(intervals[i][0] <= current[1])
{
current[1] = Math.Max(current[1], intervals[i][1]);
}
else
{
result.Add(current);
current = intervals[i];
}
}
result.Add(current);
return result.ToArray();
}
}