-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathProblem_3_IntervalsIntersections.java
38 lines (32 loc) · 1.09 KB
/
Problem_3_IntervalsIntersections.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
package Merge_Intervals;
// Problem Statement: Intervals Intersection (medium)
// LeetCode Question: 986. Interval List Intersections
import java.util.ArrayList;
import java.util.List;
public class Problem_3_IntervalsIntersections {
class Interval{
int start = 0;
int end = 0;
public Interval(int start, int end){
this.start = start;
this.end = end;
}
}
public List<Interval> merge (Interval[] arr1, Interval[] arr2){
List<Interval> result = new ArrayList<Interval>();
int i = 0, j = 0;
while(i < arr1.length && j < arr2.length){
if ((arr1[i].start >= arr2[j].start && arr1[i].start <= arr2[j].end) ||
(arr2[j].start >= arr1[i].start && arr2[j].start <= arr1[i].end)) {
result.add(new Interval(Math.max(arr1[i].start, arr2[j].start),
Math.min(arr1[i].end, arr2[j].end)));
}
if (arr1[i].end < arr2[j].end) {
i++;
} else {
j++;
}
}
return result;
}
}