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 ...
随机推荐
- 【最新】最流行的java后台框架 springmvc mybaits 集代码生成器 SSM SSH
获取[下载地址] QQ: 313596790 [免费支持更新] A 代码生成器(开发利器);全部是源码 增删改查的处理类,service层,mybatis的xml,SQL( m ...
- ios 使用GCD 多线程 教程
什么是GCD Grand Central Dispatch (GCD)是Apple开发的一个多核编程的解决方法.该方法在Mac OS X 10.6雪豹中首次推出,并随后被引入到了iOS4.0中.GCD ...
- Android开发之执行定时任务AlarmManager,Timer,Thread
1.Thread:使用线程方式2.Timer是java的特性3.AlarmManager:AlarmManager将应用与服务分割开来后,使得应用程序开发者不用 关心具体的服务,而是直接通过Alarm ...
- Android Handler 避免内存泄漏的用法总结
Android开发经常会用到handler,但是我们发现每次使用Handler都会出现:This Handler class should be static or leaks might occur ...
- hdu 1394 Minimum Inversion Number (裸树状数组 求逆序数 && 归并排序求逆序数)
题目链接 题意: 给一个n个数的序列a1, a2, ..., an ,这些数的范围是0-n-1, 可以把前面m个数移动到后面去,形成新序列:a1, a2, ..., an-1, an (where m ...
- Android之ScaleGestureDetector(缩放手势检测)
一.概述 ScaleGestureDetector这个类是专门用来检测两个手指在屏幕上做缩放的手势用的,最简单的应用就是用来缩放图片或者缩放网页. 二.要求 利用ScaleGestureDetecto ...
- Codeforces Round #269 (Div. 2)
A 题意:给出6根木棍,如果有4根相同,2根不同,则构成“bear”,如果剩余两个相同,则构成“elephant” 用一个数组分别储存各个数字出现的次数,再判断即可 注意hash[i]==5的时候,也 ...
- Delphi 712操作word
//导出Wordprocedure TFrm_Computing.ExportWord;var wordApp, WordDoc, WrdSelection, wrdtable, wrdtable1, ...
- HDU-1584 蜘蛛牌(dfs)
可以多看看. 蜘蛛牌 Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- <译>Selenium Python Bindings 5 - Waits
如今,大多数的Web应用程序使用AJAX技术.当页面加载到浏览器,页面中的元素也许在不同的时间间隔内加载.这使得元素很难定位,如果在DOM中的元素没有呈现,它将抛出ElementNotVisibleE ...