Created
November 24, 2016 14:15
-
-
Save thehoneymad/ec0b227308afe0da1e7b29575b44f068 to your computer and use it in GitHub Desktop.
This file contains 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 IList<Interval> Insert(IList<Interval> intervals, Interval newInterval) | |
{ | |
var result = new List<Interval>(); | |
foreach (var item in intervals) | |
{ | |
if (newInterval.start > item.end) | |
{ | |
result.Add(item); | |
} | |
else if (newInterval.end < item.start) | |
{ | |
result.Add(newInterval); | |
newInterval = item; | |
} | |
else if (item.end >= newInterval.start || item.start <= newInterval.end) | |
{ | |
newInterval = new Interval(Math.Min(item.start, newInterval.start), Math.Max(newInterval.end, item.end)); | |
} | |
} | |
result.Add(newInterval); | |
return result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment