-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathMergeIntervals.cs
32 lines (28 loc) · 871 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
32
using System.Collections.Generic;
using System.Linq;
//Given a collection of intervals, merge all overlapping intervals
// eg. [[1,4],[4,5]] -> [[1,5]].
namespace PracticeQuestionsSharp.Exercises.Numbers
{
public class Interval
{
public int Min { get; set; }
public int Max { get; set; }
}
public static class MergeIntervals
{
public static IList<Interval> MergeOverlappingIntervals(this IList<Interval> intervals)
{
List<Interval> sorted = intervals.OrderBy(x => x.Min).ToList();
for (int i = 0; i < sorted.Count - 1; ++i)
{
if (sorted[i].Max >= sorted[i + 1].Min)
{
sorted[i].Max = sorted[i + 1].Max;
sorted.RemoveAt(i + 1);
}
}
return sorted;
}
}
}