Leetcode: Data Stream as Disjoint Intervals && Summary of TreeMap

Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers seen so far as a list of disjoint intervals.

For example, suppose the integers from the data stream are 1, 3, 7, 2, 6, ..., then the summary will be:

[1, 1]
[1, 1], [3, 3]
[1, 1], [3, 3], [7, 7]
[1, 3], [7, 7]
[1, 3], [6, 7]
Follow up:
What if there are lots of merges and the number of disjoint intervals are small compared to the data stream's size?

TreeMap 解法:

Use TreeMap to easily find the lower and higher keys, the key is the start of the interval.
Merge the lower and higher intervals when necessary. The time complexity for adding is O(logN) since lowerKey(), higherKey(), put() and remove() are all O(logN). It would be O(N) if you use an ArrayList and remove an interval from it.

Summary of TreeMap

The map is sorted according to the natural ordering of its keys, or by a Comparator provided at map creation time, depending on which constructor is used.

This implementation provides guaranteed log(n) time cost for the containsKeygetput and remove operations.

methods include: ceilingKey(), floorKey(), higherKey(), lowerKey(), firstKey(): return the lowest key in the map, lastKey() return the highest key in the map

 /**
* Definition for an interval.
* public class Interval {
* int start;
* int end;
* Interval() { start = 0; end = 0; }
* Interval(int s, int e) { start = s; end = e; }
* }
*/
public class SummaryRanges {
TreeMap<Integer, Interval> tree; /** Initialize your data structure here. */
public SummaryRanges() {
tree = new TreeMap<>();
} public void addNum(int val) {
if (tree.containsKey(val)) return;
Integer l = tree.lowerKey(val);
Integer h = tree.higherKey(val); //case 1: val is the only number between the two intervals
if (l!=null && h!=null && val==tree.get(l).end+1 && val==h-1) {
tree.get(l).end = tree.get(h).end;
tree.remove(h);
} //case 2 & 3: val is in one interval or is the next elem of that interval's last elem
else if (l!=null && val<=tree.get(l).end+1) {
tree.get(l).end = Math.max(tree.get(l).end, val);
} //case 4: val is the first elem of a interval
else if (h!=null && val==h-1) {
tree.put(val, new Interval(val, tree.get(h).end));
tree.remove(h);
} //case 5: val does not adhere to any interval
else {
tree.put(val, new Interval(val, val));
}
} public List<Interval> getIntervals() {
return new ArrayList<>(tree.values());
}
} /**
* Your SummaryRanges object will be instantiated and called as such:
* SummaryRanges obj = new SummaryRanges();
* obj.addNum(val);
* List<Interval> param_2 = obj.getIntervals();
*/

TreeSet 解法:

 /**
* Definition for an interval.
* public class Interval {
* int start;
* int end;
* Interval() { start = 0; end = 0; }
* Interval(int s, int e) { start = s; end = e; }
* }
*/
public class SummaryRanges { /** Initialize your data structure here. */
public SummaryRanges() {
itvlSet = new TreeSet<Interval>(new Comparator<Interval>(){
public int compare(Interval v1, Interval v2){
return v1.start-v2.start;
}
}); } public void addNum(int val) {
Interval itvl = new Interval(val,val);
Interval pre = itvlSet.floor(itvl);
Interval after = itvlSet.ceiling(itvl); if ( (pre!=null && pre.end >= val) || (after!=null && after.start <=val)) return; if (pre!=null && pre.end==val-1){
itvlSet.remove(pre);
itvl.start = pre.start;
}
if (after!=null && after.start==val+1){
itvlSet.remove(after);
itvl.end = after.end;
}
itvlSet.add(itvl);
} public List<Interval> getIntervals() {
return new ArrayList<Interval>(itvlSet); } TreeSet<Interval> itvlSet;
} /**
* Your SummaryRanges object will be instantiated and called as such:
* SummaryRanges obj = new SummaryRanges();
* obj.addNum(val);
* List<Interval> param_2 = obj.getIntervals();
*/
上一篇:Spring Boot—19Session


下一篇:[LeetCode] Find Median from Data Stream 找出数据流的中位数