Skip to content

Instantly share code, notes, and snippets.

@thehoneymad
Created November 24, 2016 14:15
Show Gist options
  • Save thehoneymad/ec0b227308afe0da1e7b29575b44f068 to your computer and use it in GitHub Desktop.
Save thehoneymad/ec0b227308afe0da1e7b29575b44f068 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 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