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 ...
随机推荐
- C++RAII
http://baike.baidu.com/view/1120455.htm?fr=aladdin http://www.cnblogs.com/gnuhpc/archive/2012/12/04/ ...
- Regex Tester 安装教程
下载com.brosinski.eclipse.regex_1.4.0.jar 地址:https://github.com/sbrosinski/RegexTester 下载之后把jar包粘贴到${e ...
- linux常用头文件
http://blog.csdn.net/kokodudu/article/details/17361161 aio.h 异步I/Oassert.h 验证程序断言 complex 复数类complex ...
- Null-conditional Operators
https://msdn.microsoft.com/en-us/library/dn986595.aspx x?.y – null conditional member access. Return ...
- oracle portlist.ini
Enterprise Manager Database Control URL - (orcl) :https://redhat4.7:1158/em [root@redhat4 install]# ...
- gsp序列模式挖掘
数据挖掘进阶之序列模式挖掘GSP算法 绪 继续数据挖掘方面算法的讲解,前面讲解了数据挖掘中关联规则算法FP-Growth的实现.此篇博文主要讲解基于有趣性度量标准的GSP序列模式挖掘算法.有关论文后期 ...
- A06_RelativeLayout的属性设置
设有两个控件one和two,以控件one为基准.由于代码比较简单就不贴了,直接上效果图. 一.第一组:将控件two放在控件one的上.下.左.右.开始.结束. android:layout_below ...
- Reactor模式,或者叫反应器模式
Reactor这个词译成汉语还真没有什么合适的,很多地方叫反应器模式,但更多好像就直接叫reactor模式了,其实我觉着叫应答者模式更好理解一些.通过了解,这个模式更像一个侍卫,一直在等待你的召唤,或 ...
- bzoj2241: [SDOI2011]打地鼠
暴力. O(n^6)暴力卡过,72ms. 莫名其妙做这道题时感觉十分烦躁,难受,只能这样了. O(n^4)的方法是这样差分一下.判断的时候tmp=t[i][j],t[i][j]-=tmp,t[i+r] ...
- substring函数——sql
--substring( expression, start, length ) expression待截取的文字 start 截取位置的起始下标 length 要截取的长度 --左边第一个字符的下标 ...