generated from eyamenko/dotnet-template-repository
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem12.cs
More file actions
41 lines (35 loc) · 1.33 KB
/
Problem12.cs
File metadata and controls
41 lines (35 loc) · 1.33 KB
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
33
34
35
36
37
38
39
40
41
namespace LeetCode;
/// <summary>
/// <see href="https://leetcode.com/problems/merge-intervals/">Merge Intervals</see>.
/// </summary>
public static class Problem12
{
/// <summary>
/// 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.
/// Time complexity: O(n log n).
/// Space complexity: O(n).
/// </summary>
/// <param name="intervals">Array to traverse.</param>
/// <returns>Array of the non-overlapping intervals that cover all the intervals in the input.</returns>
public static int[][] Merge(int[][] intervals)
{
Array.Sort(intervals, Comparer<int[]>.Create((a, b) => a[0].CompareTo(b[0])));
var result = new List<int[]>(intervals.Length);
for (var i = 1; i < intervals.Length; i++)
{
var (current, previous) = (intervals[i], intervals[i - 1]);
if (current[0] <= previous[1])
{
current[0] = Math.Min(current[0], previous[0]);
current[1] = Math.Max(current[1], previous[1]);
}
else
{
result.Add(previous);
}
}
result.Add(intervals[^1]);
return result.ToArray();
}
}