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-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 ...
随机推荐
- Ossec常用命令
启动并查看httpd服务 systemctl start httpd systemctl status httpd.service 启动并查看mysql服务 systemctl start maria ...
- 8、双向一对多的关联关系(等同于双向多对一。1的一方有对n的一方的集合的引用,同时n的一方有对1的一方的引用)
双向一对多关联关系 “双向一对多关联关系”等同于“双向多对一关联关系”:1的一方有对n的一方的集合的引用,同时n的一方有对1的一方的引用. 还是用客户Customer和订单Order来解释: “一对多 ...
- 2、@RequestMapping注解的用法
@RequestMapping有如下属性值:
- PCL—低层次视觉—点云滤波(基于点云频率)
1.点云的频率 今天在阅读分割有关的文献时,惊喜的发现,点云和图像一样,有可能也存在频率的概念.但这个概念并未在文献中出现也未被使用,谨在本博文中滥用一下“高频”一词.点云表达的是三维空间中的一种信息 ...
- RestTemplateIntegrationTests
摘录RestTemplate的集成测试类/* 2. * Copyright 2002-2010 the original author or authors. 3. * 4. * L ...
- C#基础(四)
语句 到目前为止,我们的程序还只能按照编写的顺序执行,中途不能发生任何变化 ...
- git push
使用git push直接推送未关联分支的时候,出现如下提示: $ git push Counting objects: 46, done. Delta compression using up to ...
- git push default
今天使用git push的时候出现了如下提示: warning: push.default is unset; its implicit value is changing in Git 2.0 fr ...
- apache启动报错(98)Address already in use: make_sock: could not bind to...
# /etc/init.d/httpd startStarting httpd: (98)Address already in use: make_sock: could not bind to ad ...
- mysql_create_frm
http://www.cnblogs.com/jiangxu67/p/4755097.html http://www.cnblogs.com/jiangxu67/p/4755097.html http ...