-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathSolution.java
99 lines (78 loc) · 2.62 KB
/
Solution.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
public class Solution {
private static class Interval {
private int i;
private int j;
Interval(int i, int j) {
this.i = i;
this.j = j;
}
public int getI() {
return i;
}
public int getJ() {
return j;
}
public boolean isEnveloping(Interval interval) {
return getI() <= interval.getI() && getJ() >= interval.getJ();
}
@Override
public String toString() {
return "(" + getI() + ", " + getJ() + ")";
}
}
// O(n^2)
private static void solve(Interval[] intervals) {
ArrayList<Interval> solution = new ArrayList<>(intervals.length);
boolean isBeingOverlapped;
for (int i = 0; i < intervals.length; i++) {
isBeingOverlapped = false;
for (int j = 0; j < intervals.length; j++) {
if (i == j)
continue;
if (intervals[j].isEnveloping(intervals[i])) {
isBeingOverlapped = true;
break;
}
}
if (!isBeingOverlapped)
solution.add(intervals[i]);
}
System.out.println(solution);
}
// O(n logn)
private static void solve2(Interval[] intervals) {
Arrays.sort(intervals, (a, b) -> a.getI() - b.getI());
System.out.println(intervals[0]);
Interval lastPrinted = intervals[0];
for (int i = 1; i < intervals.length; i++) {
if (lastPrinted.isEnveloping(intervals[i]))
continue;
System.out.println(intervals[i]);
lastPrinted = intervals[i];
}
}
public static void main(String[] args) {
ArrayList<Interval> test = new ArrayList<>();
test.add(new Interval(1, 3));
test.add(new Interval(5, 8));
test.add(new Interval(4, 10));
test.add(new Interval(20, 25));
solve(test.toArray(new Interval[test.size()]));
System.out.println("---");
solve2(test.toArray(new Interval[test.size()]));
System.out.println("Test 2");
ArrayList<Interval> test2 = new ArrayList<>();
test2.add(new Interval(6, 8));
test2.add(new Interval(1, 9));
test2.add(new Interval(2, 4));
test2.add(new Interval(4, 7));
// (1,9)
solve(test2.toArray(new Interval[test2.size()]));
System.out.println("---");
solve2(test2.toArray(new Interval[test2.size()]));
}
}