-
Notifications
You must be signed in to change notification settings - Fork 112
/
057-InsertInterval.cs
44 lines (40 loc) · 1.36 KB
/
057-InsertInterval.cs
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
40
41
42
43
44
//-----------------------------------------------------------------------------
// Runtime: 252ms
// Memory Usage: 33.1 MB
// Link: https://leetcode.com/submissions/detail/395326352/
//-----------------------------------------------------------------------------
using System;
using System.Collections.Generic;
namespace LeetCode
{
public class _057_InsertInterval
{
public IList<Interval> Insert(IList<Interval> intervals, Interval newInterval)
{
var result = new List<Interval>();
for (int i = 0; i < intervals.Count; i++)
{
if (newInterval.start > intervals[i].end)
{
result.Add(intervals[i]);
}
else if (newInterval.end < intervals[i].start)
{
result.Add(newInterval);
for (int j = i; j < intervals.Count; j++)
{
result.Add(intervals[j]);
}
return result;
}
else
{
newInterval.start = Math.Min(intervals[i].start, newInterval.start);
newInterval.end = Math.Max(intervals[i].end, newInterval.end);
}
}
result.Add(newInterval);
return result;
}
}
}