generated from eyamenko/dotnet-template-repository
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem51.cs
More file actions
39 lines (33 loc) · 1.24 KB
/
Problem51.cs
File metadata and controls
39 lines (33 loc) · 1.24 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
namespace LeetCode;
/// <summary>
/// <see href="https://leetcode.com/problems/non-overlapping-intervals/">Non-overlapping Intervals</see>.
/// </summary>
public static class Problem51
{
/// <summary>
/// Given an array of intervals intervals where intervals[i] = [starti, endi],
/// return the minimum number of intervals you need to remove to make the rest of the intervals non-overlapping.
/// Time complexity: O(n log n).
/// Space complexity: O(log n).
/// </summary>
/// <param name="intervals">Array to traverse.</param>
/// <returns>Minimum number of intervals required to be removed to make the rest of the intervals non-overlapping.</returns>
public static int EraseOverlapIntervals(int[][] intervals)
{
Array.Sort(intervals, Comparer<int[]>.Create((a, b) => a[0].CompareTo(b[0])));
var count = 0;
for (var i = 1; i < intervals.Length; i++)
{
var (current, previous) = (intervals[i], intervals[i - 1]);
if (current[0] < previous[1])
{
if (previous[1] < current[1])
{
intervals[i] = previous;
}
count++;
}
}
return count;
}
}