-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInsert_Interval.java
More file actions
31 lines (27 loc) · 965 Bytes
/
Insert_Interval.java
File metadata and controls
31 lines (27 loc) · 965 Bytes
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
/**
* 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; }
* }
*/
public class Solution {
public ArrayList<Interval> insert(ArrayList<Interval> intervals, Interval newInterval) {
for(int i = 0;i < intervals.size();i++){
if(newInterval.end < intervals.get(i).start){
intervals.add(i,newInterval);
return intervals;
}else if(newInterval.start > intervals.get(i).end){
continue;
}else{
newInterval.start = Math.min(intervals.get(i).start,newInterval.start);
newInterval.end = Math.max(intervals.get(i).end,newInterval.end);
intervals.remove(i--);
}
}
intervals.add(newInterval);
return intervals;
}
}