LeetCode -- Insert Interval
Description:
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary ).
You may assume that the intervals were initially sorted according to their start times.
Example 1:
Given intervals [], [], insert and merge [] in as [], [].
Example 2:
Given [], [], [], [], [], [], insert and merge [] in as [], [], [12, 16].
This is because the new interval [] overlaps with [], [], [].
This article requires that for a sorted interval sequence, insert a new interval, ensure that the sequence is sorted, and merge the overlap interval.
The complexity of this question lies in the judgment of the start and end positions of the case data interval, which needs to be discussed in detail.
Ideas:
1. First, determine whether the end of the new interval I is smaller than intervals [0]. start. If yes, insert it in the first one.
2. Determine whether I. start is larger than intervals. [Count]. end.
3. Traverse Intervals. For start:
A. gap: start is located between intervals [I]. end and intervals [I + 1]. start, startIndex = I
B. between: start is located between intervals [I]. start and intervals [I]. end, startIndex = I-1;
C. start is smaller than intervals [0]. start, startIndex =-1
D. Confirm startIndex (then used to determine whether to add it to result)
After determining the start condition, start an inner loop j, [I, Count) to find the end condition:
A. gap: end is located between intervals [I]. end and intervals [I + 1]. start. endIndex = j + 1
B. start is located between intervals [I]. start and intervals [I]. end, endIndex = j + 1
C. end is larger than intervals [Count]. end, endIndex = Count + 1
D. Determine the endIndex.
4.1: add the interval between [0, startIndex] to the set
4.2: add the interval after combine to the set.
4.3: add the interval between [endIndex, Count) to the set
Implementation Code:
/** * 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
Insert(IList
intervals, Interval newInterval) { // intervals is empty , return new interval if(intervals.Count == 0){return new List
(){newInterval};}var result = new List
();if(newInterval.end < intervals[0].start){result.Add(newInterval);result.AddRange(intervals);return result;}if(newInterval.start > intervals[intervals.Count-1].end){result.AddRange(intervals);result.Add(newInterval);return result;}var newAsStart = false;if(newInterval.start < intervals[0].start){newAsStart = true;}for(var i = 0;i < intervals.Count; i++){var startInBetween = newInterval.start >= intervals[i].start && newInterval.start <= intervals[i].end;var startInGap = i < intervals.Count - 1 && newInterval.start > intervals[i].end && newInterval.start < intervals[i+1].start;//try find start fall into which interval or into which gapif(newAsStart || startInBetween || startInGap){var j = i ;// try find endvar endIndex = -1;var endInBetween = false;var endInGap = false;var endAtLast = newInterval.end > intervals[intervals.Count-1].end;if(!endAtLast){while(j < intervals.Count){if(newInterval.end >= intervals[j].start && newInterval.end <= intervals[j].end){endInBetween = true;endIndex = j;break;}else if(j < intervals.Count - 1 && newInterval.end > intervals[j].end && newInterval.end < intervals[j+1].start){endInGap = true;endIndex = j;break;}j++;}}Interval combined = null;int start = -1;int startIndex = -1;if(newAsStart){startIndex = -1;start = newInterval.start ;}else if(startInBetween){startIndex = i - 1;start = intervals[i].start;}else if(startInGap){startIndex = i;start = newInterval.start;}int end = -1;int endAt = -1;//found endif(endIndex != -1){if(endInBetween){end = intervals[endIndex].end;endAt = endIndex + 1;}else if(endInGap){end = newInterval.end;endAt = endIndex + 1;}}else if(endAtLast){end = newInterval.end;endAt = intervals.Count+1;}// not found end , means new interval end is bigger than the last end else{combined = new Interval(start, newInterval.end);endAt = intervals.Count+1;}combined = new Interval(start,end);for(var x = 0;x <= startIndex; x++){result.Add(intervals[x]);}result.Add(combined);for(var x = endAt;x < intervals.Count; x++){result.Add(intervals[(int)x]);}return result;}}//new interval start is bigger than all intervals end, just put at endresult.Add(newInterval);return result; }}
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.