-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path#1288 Remove Covered Intervals.java
41 lines (41 loc) · 1.3 KB
/
#1288 Remove Covered 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
41
//https://leetcode.com/problems/remove-covered-intervals/
class Solution {
public int removeCoveredIntervals(int[][] intervals) {
TreeMap<Integer,Integer> map = new TreeMap<>();
for(int[] pair : intervals) {
if(map.containsKey(pair[0])){
map.put(pair[0],Math.max(pair[1],map.get(pair[0])));
}
else {
try {
int prev = map.floorKey(pair[0]);
if(map.get(prev) >= pair[1]) {
continue;
}
else {
map.put(pair[0],pair[1]);
}
}
catch(Exception ex) {
map.put(pair[0],pair[1]);
}
try {
while(true) {
int next = map.ceilingKey(pair[0]+1);
if(map.get(next) <= pair[1]) {
map.remove(next);
}
else {
break;
}
}
}
catch(Exception ex) {
continue;
}
}
}
//System.out.println(map);
return map.size();
}
}