**Input**
["SummaryRanges", "addNum", "getIntervals", "addNum", "getIntervals", "addNum", "getIntervals", "addNum", "getIntervals", "addNum", "getIntervals"]
[[], [1], [], [3], [], [7], [], [2], [], [6], []]
**Output**
[null, null, [[1, 1]], null, [[1, 1], [3, 3]], null, [[1, 1], [3, 3], [7, 7]], null, [[1, 3], [7, 7]], null, [[1, 3], [6, 7]]]
**Explanation**
SummaryRanges summaryRanges = new SummaryRanges();
summaryRanges.addNum(1); // arr = [1]
summaryRanges.getIntervals(); // return [[1, 1]]
summaryRanges.addNum(3); // arr = [1, 3]
summaryRanges.getIntervals(); // return [[1, 1], [3, 3]]
summaryRanges.addNum(7); // arr = [1, 3, 7]
summaryRanges.getIntervals(); // return [[1, 1], [3, 3], [7, 7]]
summaryRanges.addNum(2); // arr = [1, 2, 3, 7]
summaryRanges.getIntervals(); // return [[1, 3], [7, 7]]
summaryRanges.addNum(6); // arr = [1, 2, 3, 6, 7]
summaryRanges.getIntervals(); // return [[1, 3], [6, 7]]
class SummaryRanges {
TreeMap<Integer, Integer> tmap;
/** Initialize your data structure here. */
public SummaryRanges() {
tmap = new TreeMap<>();
}
public void addNum(int val) {
Integer f = tmap.lowerKey(val); // floor
Integer c = tmap.ceilingKey(val); // ceil
// 4 situations
if (f != null && tmap.get(f) + 1 == val && c != null && val + 1 == c) { // concatenate both
tmap.put(f, tmap.get(c));
tmap.remove(c);
} else if (f != null && tmap.get(f) + 1 == val && (c == null || val + 1 < c)) { // concatenate floor
tmap.put(f, val);
} else if ((f == null || tmap.get(f) + 1 < val) && c != null && val + 1 == c) { // concatenate ceil
tmap.put(val, tmap.get(c));
tmap.remove(c);
} else if ((f == null || tmap.get(f) + 1 < val) && (c == null || val + 1 < c)) { // independent
tmap.put(val, val);
}
}
public List<Interval> getIntervals() {
List<Interval> res = new ArrayList<>();
for (int k : tmap.keySet()) {
res.add(new Interval(k, tmap.get(k)));
}
return res;
}
}
/**
* Your SummaryRanges object will be instantiated and called as such:
* SummaryRanges obj = new SummaryRanges();
* obj.addNum(val);
* List<Interval> param_2 = obj.getIntervals();
*/
/*
1) TreeMap, key:start, value:end; 2) when adding, check floor(val) and ceil(val); 3) 5 situations: if floor.end + 1 = val and val+1 = ceil.start, concatenate and delete ceil. concatenate both, concatenate floor, concatenate ceil, independent, already in range can't add
*/