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)的更多相关文章
-
[LeetCode] 352. Data Stream as Disjoint Intervals 分离区间的数据流
Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers seen ...
-
[leetcode]352. Data Stream as Disjoint Intervals
数据流合并成区间,每次新来一个数,表示成一个区间,然后在已经保存的区间中进行二分查找,最后结果有3种,插入头部,尾部,中间,插入头部,不管插入哪里,都判断一下左边和右边是否能和当前的数字接起来,我这样 ...
-
【leetcode】352. Data Stream as Disjoint Intervals
问题描述: Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers ...
-
352. Data Stream as Disjoint Intervals
Plz take my miserable life T T. 和57 insert interval一样的,只不过insert好多. 可以直接用57的做法一个一个加,然后如果数据大的话,要用tree ...
-
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 ...
-
352[LeetCode] Data Stream as Disjoint Intervals
Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers seen ...
-
[LeetCode] Data Stream as Disjoint Intervals 分离区间的数据流
Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers seen ...
-
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 ...
-
[Swift]LeetCode352. 将数据流变为多个不相交间隔 | Data Stream as Disjoint Intervals
Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers seen ...
随机推荐
-
解读ASP.NET 5 &; MVC6系列(15):MvcOptions配置
程序模型处理 IApplicationModelConvention 在MvcOptions的实例对象上,有一个ApplicationModelConventions属性(类型是:List<IA ...
-
Asp.Net MVC<;五>;:过滤器
ControllerActionInvoker在执行过程中除了利用ActionDescriptor完成对目标Action方法本身的执行外,还会执行相关过滤器(Filter).过滤器采用AOP的设计,它 ...
-
javascript里面的数组,json对象,动态添加,修改,删除示例
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...
-
使用plupload做一个类似qq邮箱附件上传的效果
公司项目中使用的框架是springmvc+hibernate+spring,目前需要做一个类似qq邮箱附件上传的功能,暂时只是上传小类型的附件 处理过程和解决方案都需要添加附件,处理过程和解决方案都可 ...
-
多线程基础及实例(java)
前言: 每个正在系统上运行的程序都是一个进程.每个进程包含一到多个线程.线程是一组指令的集合,或者是程序的特殊段,它可以在程序里独立执行.也可以把它理解为代码运行的上下文.所以线程基本上是轻量级的进程 ...
-
关于Binder,作为应用开发者你需要知道的全部
作者:rushjs https://www.jianshu.com/p/062a6e4f5cbe github 地址: https://github.com/rushgit/zhongwenjun.g ...
-
[译]ElasticSearch vs. Solr
在Gen2产品的早期阶段, 我们事实上是失败的, 这促使我们重新审视我们现有的技术栈. 我们仔细分析系统中的每个独立的组件,并记录下来, 当然其中也包括构成我们核心功能的搜索引擎技术. 在我们的通用日 ...
-
source insight设置tab键为4个空格
首先通过路径(Options->Document Options)进入以下界面: step 1:将 Visible tabs 打勾. step 2 :将 Expand Tabs 打勾. step ...
-
robot framework + python实现http接口自动化测试框架
https://www.jianshu.com/p/6d1e8cb90e7d 前言 下周即将展开一个http接口测试的需求,刚刚完成的java类接口测试工作中,由于之前犯懒,没有提前搭建好自动化回归测 ...
-
给浏览器和各种软件配置 http https socks5 代理 proxy
只需要像在 idea 里一样,配置好sr的本地代理ip:127.0.0.1 本地代理端口:1080 浏览器和软件的流量即可走 sr ,就能被 sr 代理了 用上代理之后,确实快了好多,这里是在打开 ...