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?

Credits:
Special thanks to @yunhong for adding this problem and creating most of the test cases.

Solution 1:

 /**
* 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() {
startMap = new HashMap<Integer, Interval>();
endMap = new HashMap<Integer, Interval>();
addedNum = new HashSet<Integer>();
} public void addNum(int val) {
if (addedNum.contains(val)) return; Interval pre = null, after = null;
if (endMap.containsKey(val - 1))
pre = endMap.get(val - 1);
if (startMap.containsKey(val + 1))
after = startMap.get(val + 1); if (pre != null && after != null) {
endMap.remove(after.end);
startMap.remove(after.start);
endMap.remove(pre.end);
pre.end = after.end;
endMap.put(pre.end, pre);
} else if (pre != null) {
endMap.remove(pre.end);
pre.end = val;
endMap.put(val, pre);
} else if (after != null) {
startMap.remove(after.start);
after.start = val;
startMap.put(val, after);
} else {
Interval single = new Interval(val, val);
endMap.put(val, single);
startMap.put(val, single);
} addedNum.add(val);
} public List<Interval> getIntervals() {
List<Interval> intervalList = new ArrayList<Interval>(endMap.values());
Collections.sort(intervalList, new Comparator<Interval>() {
public int compare(Interval v1, Interval v2) {
return v1.start - v2.start;
}
});
return intervalList;
} HashMap<Integer, Interval> startMap;
HashMap<Integer, Interval> endMap;
HashSet<Integer> addedNum;
} /**
* Your SummaryRanges object will be instantiated and called as such:
* SummaryRanges obj = new SummaryRanges();
* obj.addNum(val);
* List<Interval> param_2 = obj.getIntervals();
*/

This solution introduces many redundant: To find the "pre", we only need to find the interval with the largest start that is less than val. In short, we only need to compare Interval.start

Solution 2:

 /**
* 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的更多相关文章

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

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

  2. 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 ...

  3. 352[LeetCode] Data Stream as Disjoint Intervals

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

  4. 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 ...

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

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

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

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

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

    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. 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 ...

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

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

随机推荐

  1. GET POST 区别详解

    Get是用来从服务器上获得数据,而Post是用来向服务器上传递数据. 2. Get将表单中数据的按照variable=value的形式,添加到action所指向的URL后面,并且两者使用“?”连接,而 ...

  2. 【改进版】C++小程序中一个cout输出语句背后的堆栈知识

    最开始写这篇文章的时候,凭着自己对汇编的一点理解就堆出了这些内容,经 egmkang的指点,才发觉自己是井底之蛙,花了半天的功夫,去学习顺序点等内容.针对上次写的程序,我决定添一些内容,把程序2后面的 ...

  3. NC V6 安装目录各文件夹作用描述

    ant:存放Apache Ant,用来执行EJB的构建. bin: 存放nc部署和系统监控等命令.configsys.log部署日志(包含NC中间件.WAS中间件等部署信息)以及NC_Client文件 ...

  4. 在centos下安装django

    这里有一个不错的Django的学习资料.先收藏一下,以备后用.谢谢 http://www.ziqiangxuetang.com/django/django-install.html 在centos下安 ...

  5. 比较合并工具vimdiff的主要用法归纳

    参考:https://www.ibm.com/developerworks/cn/linux/l-vimdiff/ vimdiff主要用法归纳如下:   1.打开文件 vimdiff file1 fi ...

  6. 定时重启Apache与MySQL方法

    可以定时重启apache服务器等.让网站运行的效果更快. 采用at命令添加计划任务. 有关使用语法可以到window->“开始”->运行“cmd”->执行命令“at /”,这样界面中 ...

  7. mysql 语句其它及优化

    将检索到的数据保存到文件  Select * into outfile ‘文件地址’ from tabname; 生成的文件以制表符区分字段,以换行符区分记录 为满足特殊需求会采用不同的分割方式. 支 ...

  8. php对数组排序的例子

    分享一个php数组排序的例子,介绍了和php,有关的知识.技巧.经验,和一些php源码等. <?php class='pingjiaF' frameborder='0' src='http:// ...

  9. QQ分组实现,可收缩---ExpandableListView

    activity: package com.zzw.qqgroup; import java.util.ArrayList; import java.util.HashMap; import java ...

  10. VPS centos 6 安装图形界面

    在某种场合之下,我们使用的Linux还是要选择安装桌面环境的,所以在这里介绍一下如何给没有安装桌面环境的系统安装桌面环境.以Centos 6.5 为例演示一下如何安装桌面环境. 工具/原料 Linux ...