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 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?
TreeMap 解法:
Use TreeMap to easily find the lower and higher keys, the key is the start of the interval.
Merge the lower and higher intervals when necessary. The time complexity for adding is O(logN) since lowerKey(), higherKey(), put() and remove() are all O(logN). It would be O(N) if you use an ArrayList and remove an interval from it.
Summary of TreeMap
The map is sorted according to the natural ordering of its keys, or by a Comparator
provided at map creation time, depending on which constructor is used.
This implementation provides guaranteed log(n) time cost for the containsKey
, get
, put
and remove
operations.
methods include: ceilingKey(), floorKey(), higherKey(), lowerKey(), firstKey(): return the lowest key in the map, lastKey() return the highest key in the map
/**
* 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; }
* }
*/
public class SummaryRanges {
TreeMap<Integer, Interval> tree; /** Initialize your data structure here. */
public SummaryRanges() {
tree = new TreeMap<>();
} public void addNum(int val) {
if (tree.containsKey(val)) return;
Integer l = tree.lowerKey(val);
Integer h = tree.higherKey(val); //case 1: val is the only number between the two intervals
if (l!=null && h!=null && val==tree.get(l).end+1 && val==h-1) {
tree.get(l).end = tree.get(h).end;
tree.remove(h);
} //case 2 & 3: val is in one interval or is the next elem of that interval's last elem
else if (l!=null && val<=tree.get(l).end+1) {
tree.get(l).end = Math.max(tree.get(l).end, val);
} //case 4: val is the first elem of a interval
else if (h!=null && val==h-1) {
tree.put(val, new Interval(val, tree.get(h).end));
tree.remove(h);
} //case 5: val does not adhere to any interval
else {
tree.put(val, new Interval(val, val));
}
} public List<Interval> getIntervals() {
return new ArrayList<>(tree.values());
}
} /**
* Your SummaryRanges object will be instantiated and called as such:
* SummaryRanges obj = new SummaryRanges();
* obj.addNum(val);
* List<Interval> param_2 = obj.getIntervals();
*/
TreeSet 解法:
/**
* 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; }
* }
*/
public class SummaryRanges { /** Initialize your data structure here. */
public SummaryRanges() {
itvlSet = new TreeSet<Interval>(new Comparator<Interval>(){
public int compare(Interval v1, Interval v2){
return v1.start-v2.start;
}
}); } public void addNum(int val) {
Interval itvl = new Interval(val,val);
Interval pre = itvlSet.floor(itvl);
Interval after = itvlSet.ceiling(itvl); if ( (pre!=null && pre.end >= val) || (after!=null && after.start <=val)) return; if (pre!=null && pre.end==val-1){
itvlSet.remove(pre);
itvl.start = pre.start;
}
if (after!=null && after.start==val+1){
itvlSet.remove(after);
itvl.end = after.end;
}
itvlSet.add(itvl);
} public List<Interval> getIntervals() {
return new ArrayList<Interval>(itvlSet); } TreeSet<Interval> itvlSet;
} /**
* Your SummaryRanges object will be instantiated and called as such:
* SummaryRanges obj = new SummaryRanges();
* obj.addNum(val);
* List<Interval> param_2 = obj.getIntervals();
*/
Leetcode: Data Stream as Disjoint Intervals && Summary of TreeMap的更多相关文章
- [LeetCode] Data Stream as Disjoint Intervals 分离区间的数据流
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@ [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-ne ...
- [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
问题描述: Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers ...
- [Swift]LeetCode352. 将数据流变为多个不相交间隔 | Data Stream as Disjoint Intervals
Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers seen ...
- 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. Data Stream as Disjoint Intervals
Plz take my miserable life T T. 和57 insert interval一样的,只不过insert好多. 可以直接用57的做法一个一个加,然后如果数据大的话,要用tree ...
- [leetcode]352. Data Stream as Disjoint Intervals
数据流合并成区间,每次新来一个数,表示成一个区间,然后在已经保存的区间中进行二分查找,最后结果有3种,插入头部,尾部,中间,插入头部,不管插入哪里,都判断一下左边和右边是否能和当前的数字接起来,我这样 ...
随机推荐
- 如何用Apache TCPMon来截获SOAP消息
在WebService服务器和客户机之间会传递SOAP消息,有时我们需要得到这些消息以便调试,而Apache的TCPMon可以帮助我们做到这一点. TCPMon的下载地址在http://ws.apa ...
- Object-relational mapping
https://en.wikipedia.org/wiki/Object-relational_mapping Object-relational mapping (ORM, O/RM, and O/ ...
- ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/
https://en.wikipedia.org/wiki/Base64 The Base64 index table: Value Char Value Char Value Char ...
- jsmooth 中文乱码
为了一个问题 语言国际国际化 测试了这么多回 ,真佩服自己 jsmooth 中文乱码 语言乱码 的解决办法 : 需要在“JVM” 的参数 中填入一项 : user.language=en 而不是 ...
- linux 不能用clock 计算sleep的时间
http://bbs.csdn.net/topics/390558707 在Windows Sleep()占用processor time,Linux下的sleep()不占用processor tim ...
- hitTest:WithEvent 和Responder Chain
这个方法是找到那个View被touch,当找到后就成为响应链的第一个了,如果他不能处理这个Event,那么就找nextResponder 直至application 如果不能处理,那就会丢弃掉. ht ...
- 报javax.servlet.ServletException: Servlet.init() for servlet springmvc threw exception异常 的解决方案
后台错误信息如下: javax.servlet.ServletException: Servlet.init() for servlet springmvc threw exception org.a ...
- ADO.NET中主要对象
ADO.NET是什么? ADO.NET是.Net平台提供和数据库交互的类库集,我们可以通过它对SQLSERVER,XML,Oracle等这样的数据源进行访问. 应用程序可以使用ADO.NET链接到数据 ...
- C/C++ 排序&&查找算法(面试)
一.排序 1.冒泡排序 void BubbleSort(int array[],int n) { ; ; ; ; ;i<n - ;i++) /*外循环控制排序的总趟数*/ { flag = ; ...
- Android笔记:真机调试无法输出Log 信息的问题
机器在出厂时将log的级别做了限制,方法是:拨号盘输入*20121220# -> 选择日志输出级别 -> 选择Java log level -> 选择LOGD即可. 方法是:拨号盘输 ...