forked from szl0072/Leetcode-Solution-Code
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDataStreamasDisjointIntervals.java
55 lines (45 loc) · 1.55 KB
/
DataStreamasDisjointIntervals.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
package leetcode;
import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;
/**
* Project Name : Leetcode
* Package Name : leetcode
* File Name : DataStreamasDisjointIntervals
* Creator : Edward
* Date : Jan, 2018
* Description : 352. Data Stream as Disjoint Intervals
*/
public class DataStreamasDisjointIntervals {
/**
treeMap : (0,3) (7,7)
lowerKey = null
higherKey = 1
val = 0
*/
TreeMap<Integer, Interval> treeMap;
public DataStreamasDisjointIntervals() {
treeMap = new TreeMap<>();
}
// time : O(logn)
public void addNum(int val) {
if (treeMap.containsKey(val)) return;
Integer lowerKey = treeMap.lowerKey(val);
Integer higherKey = treeMap.higherKey(val);
if (lowerKey != null && higherKey != null && treeMap.get(lowerKey).end + 1 == val
&& val + 1 == treeMap.get(higherKey).start) {
treeMap.get(lowerKey).end = treeMap.get(higherKey).end;
treeMap.remove(higherKey);
} else if (lowerKey != null && val <= treeMap.get(lowerKey).end + 1) {
treeMap.get(lowerKey).end = Math.max(treeMap.get(lowerKey).end, val);
} else if (higherKey != null && treeMap.get(higherKey).start - 1 == val) {
treeMap.put(val, new Interval(val, treeMap.get(higherKey).end));
treeMap.remove(higherKey);
} else {
treeMap.put(val, new Interval(val, val));
}
}
public List<Interval> getIntervals() {
return new ArrayList<>(treeMap.values());
}
}