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. SQL 数据库 复制 与订阅 实现数据同步

    摘自: http://www.jb51.net/article/18039.htm

  2. div紧靠浏览器底部

    <body> <div class='bottom'> 这个div紧贴浏览器底部,且居中显示 </div> </body> css代码: .bottom ...

  3. javascript之delete操作符

    理解delete 理论 代码段的类型 执行上下文 活动对象 / 变量对象 属性的特性 内置属性与 DontDelete 未声明的赋值 Firebug的困惑 在eval中删除变量 浏览器兼容性 Geck ...

  4. 开始记录blog

    将自己的总结.新的记录下来,形成习惯,为以后的温故知新

  5. 鼠标HOVER时区块动画旋转变色的CSS3样式掩码

    鼠标hover时区块动画旋转变色的css3样式掩码<!DOCTYPE html> <html> <head> <meta charset="utf- ...

  6. "/Date(1405056837780)/" 时间转换

    //往往json传过来的时间都是"/Date(1405056837780)/" //转换需要的方法 String.prototype.ToString = function (fo ...

  7. yii在TbGridView的td里面加入相应的下拉选项(转)

    当你需要在一个GridView渲染某一个复杂元素的时候(特别是在这种情况下,这是一个小部件),这就需要你在控制器中调用一个动作.例如你给一个GridView的定义这样的一列: <?php $th ...

  8. Android工程目录及其作用简介

    1. src:存放所有的*.java源程序. 2. gen:为ADT插件自动生成的代码文件保存路径,里面的R.java将保存所有的资源ID. 3. assets:可以存放项目一些较大的资源文件,例如: ...

  9. PHP JS HTML ASP页面跳转代码 延时跳转代码

    1.PHP延时跳转代码 //跳转到浏览界面 header("Refresh:1;url=machine_list.php"); //不延时 <?php header(&quo ...

  10. C# 多线程 简单使用方法以及常用参数

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...