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();
*/

Leetcode: Data Stream as Disjoint Intervals && Summary of TreeMap的更多相关文章

  1. [LeetCode] Data Stream as Disjoint Intervals 分离区间的数据流

    Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers seen ...

  2. 352[LeetCode] Data Stream as Disjoint Intervals

    Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers seen ...

  3. leetcode@ [352] Data Stream as Disjoint Intervals (Binary Search & TreeSet)

    https://leetcode.com/problems/data-stream-as-disjoint-intervals/ Given a data stream input of non-ne ...

  4. [LeetCode] 352. Data Stream as Disjoint Intervals 分离区间的数据流

    Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers seen ...

  5. 【leetcode】352. Data Stream as Disjoint Intervals

    问题描述: Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers ...

  6. [Swift]LeetCode352. 将数据流变为多个不相交间隔 | Data Stream as Disjoint Intervals

    Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers seen ...

  7. 352. Data Stream as Disjoint Intervals (TreeMap, lambda, heapq)

    Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers seen ...

  8. 352. Data Stream as Disjoint Intervals

    Plz take my miserable life T T. 和57 insert interval一样的,只不过insert好多. 可以直接用57的做法一个一个加,然后如果数据大的话,要用tree ...

  9. [leetcode]352. Data Stream as Disjoint Intervals

    数据流合并成区间,每次新来一个数,表示成一个区间,然后在已经保存的区间中进行二分查找,最后结果有3种,插入头部,尾部,中间,插入头部,不管插入哪里,都判断一下左边和右边是否能和当前的数字接起来,我这样 ...

随机推荐

  1. spotlight监控工具使用

    利用spotlight工具可以监控如下系统:        1.Spotlight on Unix 监控Linux服务器 1)安装 Spotlight on Unix 2)配置spotlight登陆用 ...

  2. processor, memory, I/O

    COMPUTER ORGANIZATION AND ARCHITECTURE DESIGNING FOR PERFORMANCE NINTH EDITION 3.3 INTERCONNECTION S ...

  3. P1965 转圈游戏

    很容易可以得到,答案应该是(x+m*10^k)%n 很显然,用O(n)一定会卡爆,所以用快速幂来算,或者找一下循环节也是可以的. #include <bits/stdc++.h> usin ...

  4. 用Delphi“遥控”按钮

    很多情况下,我们需要在程序中实现这样的功能:在自编写的程序里控制另外一软件中的某个按钮被按下.比如,有一天你在聊QQ时觉得烦了,那么就想写程序来帮你按下“发送”按钮,省得你自己一次次动手了.那么,这个 ...

  5. 七 mybatis的延迟加载

    1       延迟加载 1.1     什么是延迟加载 resultMap可以实现高级映射(使用association.collection实现一对一及一对多映射),association.coll ...

  6. 使用AJAX做关键字查询:输入框变化自动搜索、无刷新页面;

    使用AJAX做关键字查询要求:1.无刷新页面2.输入框变化自动搜索 <style type="text/css"> .k{ width:150px; height:30 ...

  7. Architecture of a Highly Scalable NIO-Based Server

    一. thread-per-connection The thread-per-connection approach uses an exclusive worker thread for each ...

  8. Qt自定义model

    前面我们说了Qt提供的几个预定义model.但是,面对变化万千的需求,那几个model是远远不能满足我们的需要的.另外,对于Qt这种框架来说,model的选择首先要能满足绝大多数功能的需要,这就是说, ...

  9. 如何查看Servlet、JSP的版本(Tomcat V7.0.70)

    1. 简要说明:Tomcat6.0 所支持的是Servlet2.5,Tomcat 7.0 所支持的Servlet3.0,Servlet2.5 和Servlet3.0的差异较大,对于Servlet3.0 ...

  10. 由单例模式学到:Lazy<T>

    http://www.cnblogs.com/zhangpengshou/archive/2012/12/10/2811765.html http://www.cnblogs.com/anytao/a ...