-
Notifications
You must be signed in to change notification settings - Fork 0
/
insert intervals.java
40 lines (33 loc) · 1.33 KB
/
insert intervals.java
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
/**
* Definition for an interval.
* public class Interval {
* int start;
* int end;
* Interval() { start = 0; end = 0; }
* Interval(int s, int e) { start = s; end = e; }
* }
*/
// the solution is not hard to come update
// two ways:
// 1. change intervals itself
// make a new empty space to store the new arraylist
// when updating intervals, i should be i-- and insert the new interval into right place
public class Solution {
public List<Interval> insert(List<Interval> intervals, Interval newInterval) {
if(newInterval == null){
return intervals;
}
int i;
for(i = 0; i < intervals.size(); i++) {
Interval curr = intervals.get(i);
if (curr.end < newInterval.start) continue; //nonoverlap, newInterval is behind current, continue;
if(newInterval.end < curr.start) break; //nonoverlap, newInterval is before current, jump out of loop
newInterval.start = Math.min(curr.start, newInterval.start); //overlap, update newInterval and remove current
newInterval.end = Math.max(curr.end, newInterval.end);
intervals.remove(i);
i--;
}
intervals.add(i, newInterval);
return intervals;
}
}