博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Leetcode: Data Stream as Disjoint Intervals && Summary of TreeMap
阅读量:6071 次
发布时间:2019-06-20

本文共 4547 字,大约阅读时间需要 15 分钟。

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  of its keys, or by a  provided at map creation time, depending on which constructor is used.

This implementation provides guaranteed log(n) time cost for the containsKeygetput 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

1 /** 2  * Definition for an interval. 3  * public class Interval { 4  *     int start; 5  *     int end; 6  *     Interval() { start = 0; end = 0; } 7  *     Interval(int s, int e) { start = s; end = e; } 8  * } 9  */10 public class SummaryRanges {11     TreeMap
tree;12 13 /** Initialize your data structure here. */14 public SummaryRanges() {15 tree = new TreeMap<>();16 }17 18 public void addNum(int val) {19 if (tree.containsKey(val)) return;20 Integer l = tree.lowerKey(val);21 Integer h = tree.higherKey(val);22 23 //case 1: val is the only number between the two intervals24 if (l!=null && h!=null && val==tree.get(l).end+1 && val==h-1) {25 tree.get(l).end = tree.get(h).end;26 tree.remove(h);27 }28 29 //case 2 & 3: val is in one interval or is the next elem of that interval's last elem30 else if (l!=null && val<=tree.get(l).end+1) {31 tree.get(l).end = Math.max(tree.get(l).end, val);32 }33 34 //case 4: val is the first elem of a interval35 else if (h!=null && val==h-1) {36 tree.put(val, new Interval(val, tree.get(h).end));37 tree.remove(h);38 }39 40 //case 5: val does not adhere to any interval41 else {42 tree.put(val, new Interval(val, val));43 }44 }45 46 public List
getIntervals() {47 return new ArrayList<>(tree.values());48 }49 }50 51 /**52 * Your SummaryRanges object will be instantiated and called as such:53 * SummaryRanges obj = new SummaryRanges();54 * obj.addNum(val);55 * List
param_2 = obj.getIntervals();56 */

 

TreeSet 解法:

1 /** 2  * Definition for an interval. 3  * public class Interval { 4  *     int start; 5  *     int end; 6  *     Interval() { start = 0; end = 0; } 7  *     Interval(int s, int e) { start = s; end = e; } 8  * } 9  */10 public class SummaryRanges {11 12     /** Initialize your data structure here. */13     public SummaryRanges() {14         itvlSet = new TreeSet
(new Comparator
(){15 public int compare(Interval v1, Interval v2){16 return v1.start-v2.start;17 }18 });19 20 }21 22 public void addNum(int val) {23 Interval itvl = new Interval(val,val);24 Interval pre = itvlSet.floor(itvl);25 Interval after = itvlSet.ceiling(itvl);26 27 if ( (pre!=null && pre.end >= val) || (after!=null && after.start <=val)) return;28 29 if (pre!=null && pre.end==val-1){30 itvlSet.remove(pre);31 itvl.start = pre.start;32 }33 if (after!=null && after.start==val+1){34 itvlSet.remove(after);35 itvl.end = after.end;36 }37 itvlSet.add(itvl);38 }39 40 public List
getIntervals() {41 return new ArrayList
(itvlSet);42 43 }44 45 TreeSet
itvlSet;46 }47 48 /**49 * Your SummaryRanges object will be instantiated and called as such:50 * SummaryRanges obj = new SummaryRanges();51 * obj.addNum(val);52 * List
param_2 = obj.getIntervals();53 */

 

转载地址:http://lkigx.baihongyu.com/

你可能感兴趣的文章
Linux下基本栈溢出攻击【转】
查看>>
c# 连等算式都在做什么
查看>>
使用c:forEach 控制5个换行
查看>>
java web轻量级开发面试教程摘录,java web面试技巧汇总,如何准备Spring MVC方面的面试...
查看>>
根据调试工具看Vue源码之组件通信(一)
查看>>
Thrift RPC 系列教程(5)—— 接口设计篇:struct & enum设计
查看>>
斯坦福-随机图模型-week1.5
查看>>
灵活的运用Model类
查看>>
hadoop 之分布式安装
查看>>
使用ansible工具部署ceph
查看>>
linux系列博文---->深入理解linux启动运行原理(一)
查看>>
Android反编译(一) 之反编译JAVA源码
查看>>
结合当前公司发展情况,技术团队情况,设计一个适合的技术团队绩效考核机制...
查看>>
python-45: opener 的使用
查看>>
cad图纸转换完成的pdf格式模糊应该如何操作?
查看>>
Struts2与Struts1区别
查看>>
网站内容禁止复制解决办法
查看>>
Qt多线程
查看>>
我的友情链接
查看>>
Ubuntu12.04 编译android源代码及生成模拟器经历分享
查看>>