Skip to content

Instantly share code, notes, and snippets.

@thehoneymad
Created November 24, 2016 19:15
Show Gist options
  • Select an option

  • Save thehoneymad/541a1d51f3c0f8a3fcb34784727874a5 to your computer and use it in GitHub Desktop.

Select an option

Save thehoneymad/541a1d51f3c0f8a3fcb34784727874a5 to your computer and use it in GitHub Desktop.
/**
* Definition for an interval.
* public class Interval {
* public int start;
* public int end;
* public Interval() { start = 0; end = 0; }
* public Interval(int s, int e) { start = s; end = e; }
* }
*/
public class Solution {
public int EraseOverlapIntervals(Interval[] intervals) {
if (intervals.Length <= 1)
return 0;
intervals = intervals.OrderBy(x => x.start).ToArray();
int count = 0;
Interval last = intervals.FirstOrDefault();
if (last != null)
{
int minEnd = last.end;
for (int i = 1; i < intervals.Length; i++)
{
if (intervals[i].start < minEnd)
{
count++;
minEnd = Math.Min(intervals[i].end, minEnd);
}
else if (intervals[i].start >= minEnd)
{
minEnd = intervals[i].end;
}
}
}
return count;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment