2626 */
2727public class InsertInterval {
2828
29- /**
30- * Array. In-place solution.
31- * Skip the non-overlapping intervals whose end time is before new interval's start.
32- * For overlapped intervals that start before new interval end.
33- * | Remove this overlapped interval from list.
34- * | Merge it with the new interval by: 1. update start to min start times; 2. update end to max end times.
35- * Add new interval in the position i.
36- */
37- public List <Interval > insert (List <Interval > intervals , Interval newInterval ) {
38- int i = 0 ;
39- // Skip the non-overlapping intervals.
40- while (i < intervals .size () && intervals .get (i ).end < newInterval .start ) {
41- i ++;
42- }
43- // Now i == intervals.size() OR intervals.get(i).end >= newInterval.start
44- while (i < intervals .size () && intervals .get (i ).start <= newInterval .end ) { // Compare current interval with merged new interval.
45- Interval inter = intervals .remove (i ); // Remove the overlapped interval.
46- newInterval .start = Math .min (inter .start , newInterval .start );
47- newInterval .end = Math .max (inter .end , newInterval .end );
48- }
49- intervals .add (i , newInterval );
50- return intervals ;
29+ /**
30+ * Array. In-place solution.
31+ * Skip the non-overlapping intervals whose end time is before new interval's start.
32+ * For overlapped intervals that start before new interval end.
33+ * | Remove this overlapped interval from list.
34+ * | Merge it with the new interval by: 1. update start to min start times; 2. update end to max end times.
35+ * Add new interval in the position i.
36+ */
37+ public List <Interval > insert (List <Interval > intervals , Interval newInterval ) {
38+ int i = 0 ;
39+ // Skip the non-overlapping intervals.
40+ while (i < intervals .size () && intervals .get (i ).end < newInterval .start ) {
41+ i ++;
5142 }
43+ // Now i == intervals.size() OR intervals.get(i).end >= newInterval.start
44+ while (i < intervals .size () && intervals .get (i ).start <= newInterval .end ) { // Compare current interval with merged new interval.
45+ Interval inter = intervals .remove (i ); // Remove the overlapped interval.
46+ newInterval .start = Math .min (inter .start , newInterval .start );
47+ newInterval .end = Math .max (inter .end , newInterval .end );
48+ }
49+ intervals .add (i , newInterval );
50+ return intervals ;
51+ }
5252
53- /**
54- * Array. Sort. Not in place solution. O(n) Time.
55- * Make use of intervals are sorted.
56- * Create a result list of intervals, res.
57- * Add new interval to res first.
58- * Go through the given intervals.
59- * For each interval i, there are 3 situations:
60- * 1) i and previous interval not overlapping, in front of the new interval.
61- * 2) No overlap, behind the new interval
62- * 3) Overlap, merge with the new interval
63- */
64- public List <Interval > insertB (List <Interval > intervals , Interval newInterval ) {
65- List <Interval > res = new LinkedList <>();
66- res .add (newInterval );
67- for (Interval i : intervals ) {
68- int start = res .get (res .size () - 1 ).start ; // Last interval.
69- int end = res .get (res .size () - 1 ).end ;
70- if (i .end < start ) { // i ends before last interval.
71- res .add (res .size () - 1 , i ); // Insert it before last interval.
72- } else if (end < i .start ) { // i starts after last interval.
73- res .add (i ); // Append it to last.
74- } else { // i overlaps with last interval.
75- start = Math .min (start , i .start );
76- end = Math .max (end , i .end );
77- res .set (res .size () - 1 , new Interval (start , end ));
78- }
79- }
80- return res ;
53+ /**
54+ * Array. Sort. Not in place solution. O(n) Time.
55+ * Make use of intervals are sorted.
56+ * Create a result list of intervals, res.
57+ * Add new interval to res first.
58+ * Go through the given intervals.
59+ * For each interval i, there are 3 situations:
60+ * 1) i and previous interval not overlapping, in front of the new interval.
61+ * 2) No overlap, behind the new interval
62+ * 3) Overlap, merge with the new interval
63+ */
64+ public List <Interval > insertB (List <Interval > intervals , Interval newInterval ) {
65+ List <Interval > res = new LinkedList <>();
66+ res .add (newInterval );
67+ for (Interval i : intervals ) {
68+ int start = res .get (res .size () - 1 ).start ; // Last interval.
69+ int end = res .get (res .size () - 1 ).end ;
70+ if (i .end < start ) { // i ends before last interval.
71+ res .add (res .size () - 1 , i ); // Insert it before last interval.
72+ } else if (end < i .start ) { // i starts after last interval.
73+ res .add (i ); // Append it to last.
74+ } else { // i overlaps with last interval.
75+ start = Math .min (start , i .start );
76+ end = Math .max (end , i .end );
77+ res .set (res .size () - 1 , new Interval (start , end ));
78+ }
8179 }
80+ return res ;
81+ }
8282}
0 commit comments