https://leetcode.com/problems/data-stream-as-disjoint-intervals/

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?

/**
* 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; }
* }
*/
class cmp implements Comparator {
public int compare(Object o1, Object o2) {
Interval i1 = (Interval) o1;
Interval i2 = (Interval) o2; if(i1.start < i2.start) {
return -1;
} else if(i1.start == i2.start) {
if(i1.end == i2.end) {
return 0;
} else if(i1.end < i2.end) {
return -1;
} else {
return 1;
}
} else {
return 1;
}
}
}
public class SummaryRanges { public Set<Interval> interval_pool = null; /** Initialize your data structure here. */
public SummaryRanges() {
interval_pool = new TreeSet<Interval> (new cmp());
} public boolean binarySearch(Object[] ls, int key) {
int l = 0, r = ls.length-1; while(l <= r) {
int mid = (l + r) / 2;
Interval it = (Interval) ls[mid];
if(key <= it.end && key >= it.start) {
return true;
} else if(key > it.end) {
l = mid+1;
} else {
r = mid-1;
}
} return false;
} public Interval leftAdjacent(Object[] ls, int key) {
int l = 0, r = ls.length-1; while(l <= r) {
int mid = (l + r) / 2;
Interval it = (Interval) ls[mid];
if(key == it.end) {
return it;
} else if(key > it.start) {
l = mid+1;
} else {
r = mid-1;
}
} return null;
} public Interval rightAdjacent(Object[] ls, int key) {
int l = 0, r = ls.length-1; while(l <= r) {
int mid = (l + r) / 2;
Interval it = (Interval) ls[mid];
if(key == it.start) {
return it;
} else if(key > it.start) {
l = mid+1;
} else {
r = mid-1;
}
} return null;
} public void addNum(int val) { if(interval_pool.size() == 0) {
interval_pool.add(new Interval(val, val));
return;
} Object[] ls = interval_pool.toArray();
boolean in = binarySearch(ls, val); if(!in) {
int start = val, end = val; Interval l_adj = leftAdjacent(ls, val-1);
Interval r_adj = rightAdjacent(ls, val+1); if(l_adj != null) {
start = l_adj.start;
interval_pool.remove(l_adj);
}
if(r_adj != null) {
end = r_adj.end;
interval_pool.remove(r_adj);
} Interval it = new Interval(start, end);
interval_pool.add(it);
}
} public List<Interval> getIntervals() {
List<Interval> rs = new ArrayList<Interval> ();
Object[] ls = interval_pool.toArray();
for(int i=0; i<ls.length; ++i) {
Interval it = (Interval) ls[i];
rs.add(it);
}
return rs;
}
} /**
* Your SummaryRanges object will be instantiated and called as such:
* SummaryRanges obj = new SummaryRanges();
* obj.addNum(val);
* List<Interval> param_2 = obj.getIntervals();
*/

leetcode@ [352] Data Stream as Disjoint Intervals (Binary Search & TreeSet)的更多相关文章

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

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

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

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

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

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

  4. 352. Data Stream as Disjoint Intervals

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

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

  6. 352[LeetCode] Data Stream as Disjoint Intervals

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

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

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

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

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

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

随机推荐

  1. js 字符串日期 转成 Date

    只支持 2015/09/23 反斜杠这样类型 2015-09-23 单横的这种无法识别 var dateStr='${endDate}'; dateStr=dateStr.replace(/-/g,' ...

  2. android中给TextView或者Button的文字添加阴影效果

    1在代码中添加文字阴影 TextView 有一个方法 /**      * Gives the text a shadow of the specified radius and color, the ...

  3. C#路径/文件/目录/I/O常见操作汇总<转载>

    文件操作是程序中非常基础和重要的内容,而路径.文件.目录以及I/O都是在进行文件操作时的常见主题,这里想把这些常见的问题作个总结,对于每个问题,尽量提供一些解决方案,即使没有你想要的答案,也希望能提供 ...

  4. BZOJ2482: [Spoj1557] Can you answer these queries II

    题解: 从没见过这么XXX的线段树啊... T_T 我们考虑离线做,按1-n一个一个插入,并且维护区间[ j,i](i为当前插入的数)j<i的最优值. 但这个最优值!!! 我们要保存历史的最优值 ...

  5. 出现错误ActivityManager: Warning: Activity not started, its current task has been

    1.在学习两个Activity的切换时,重新把新的工程部署上模拟器时候出现错误:ActivityManager: Warning: Activity not started, its current ...

  6. Java Web编程的主要组件技术——Struts入门

    参考书籍:<J2EE开源编程精要15讲> Struts是一个开源的Java Web框架,很好地实现了MVC设计模式.通过一个配置文件,把各个层面的应用组件联系起来,使组件在程序层面联系较少 ...

  7. 使php支持mbstring库

    多国语言并存就意味着多字节,PHP内置的字符串长度函数strlen无法正确处理中文字符串,它得到的只是字符串所占的字节数.对于GB2312的中文编码,strlen得到的值是汉字个数的2倍,而对于UTF ...

  8. erlang 线上分析工具集锦

    1.Recon-Erlang线上系统诊断工具(引自): Erlang系统素以稳定可靠闻名,但是它也是c实现的,也是要管理比如内存,锁等等复杂的事情,也会出现Crash,而且crash的时候大部分原因是 ...

  9. 【WEB】jsp向servlet传参中文乱码问题解决

    传参方式:POST.GET.link方式 servlet向jsp传中文参数msg if(username.equals("") || password.euqals("& ...

  10. BasicDataSource配备

    BasicDataSource配置 commons DBCP 配置参数简要说明 前段时间因为项目原因,要在修改数据库连接池到DBCP上,折腾了半天,有一点收获,不敢藏私,特在这里与朋友们共享. 在配置 ...