-
Notifications
You must be signed in to change notification settings - Fork 112
/
056-MergeIntervals.cs
43 lines (38 loc) · 1.22 KB
/
056-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
33
34
35
36
37
38
39
40
41
42
43
//-----------------------------------------------------------------------------
// Runtime: 256ms
// Memory Usage: 33.3 MB
// Link: https://leetcode.com/submissions/detail/375127990/
//-----------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
namespace LeetCode
{
public class _056_MergeIntervals
{
public int[][] Merge(int[][] intervals)
{
if (intervals.Length <= 1) return intervals;
Array.Sort(intervals, (a, b) =>
{
var comp = a[0].CompareTo(b[0]);
if (comp == 0)
comp = a[1].CompareTo(b[1]);
return comp;
});
var result = new List<int[]>();
result.Add(intervals[0]);
for (int i = 1; i < intervals.Length; i++)
{
if (result.Last()[1] >= intervals[i][0])
{
if (result.Last()[1] < intervals[i][1])
result.Last()[1] = intervals[i][1];
}
else
result.Add(intervals[i]);
}
return result.ToArray(); ;
}
}
}