Created
November 24, 2016 19:15
-
-
Save thehoneymad/541a1d51f3c0f8a3fcb34784727874a5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * 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