mirror of
https://gitlab.com/mfocko/LeetCode.git
synced 2024-11-10 00:09:06 +01:00
cs: add «57. Insert Interval»
Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
parent
c228a24b1a
commit
e9815ea0aa
1 changed files with 27 additions and 0 deletions
27
cs/insert-interval.cs
Normal file
27
cs/insert-interval.cs
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
public class Solution {
|
||||||
|
private static int[] Merge(int[] l, int[] r) {
|
||||||
|
return new int[] { Math.Min(l[0], r[0]), Math.Max(l[1], r[1]) };
|
||||||
|
}
|
||||||
|
|
||||||
|
public int[][] Insert(int[][] intervals, int[] newInterval) {
|
||||||
|
var newIntervals = new List<int[]>();
|
||||||
|
|
||||||
|
var i = 0;
|
||||||
|
for (; i < intervals.Length && intervals[i][1] < newInterval[0]; ++i) {
|
||||||
|
newIntervals.Add(intervals[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (; i < intervals.Length && newInterval[1] >= intervals[i][0]; ++i) {
|
||||||
|
newInterval = Merge(newInterval, intervals[i]);
|
||||||
|
}
|
||||||
|
newIntervals.Add(newInterval);
|
||||||
|
|
||||||
|
for (; i < intervals.Length; ++i) {
|
||||||
|
newIntervals.Add(intervals[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
return newIntervals.ToArray();
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue