352. Data Stream as Disjoint Intervals
刷
June-20-2019
以前做的有问题,感觉遇到重复的会错,但是parameter变了,不好测了= =
这个题还是值得记一下的。其实是插入多个[1,1]这种interval。无非是跟前后判断就那么几种情况:
- 左右相连,那一起MREGE,然后删掉后面的
- 只和一边连,
- 都不连
- 其中一边囊括,
更重要等是一些general java 的相关
TreeMap
- Key是排序的,可自定义comparator
- lowerKey(), higherKey() 返还上一个,下一个存在的key
- get(), remove(), containsKey(), lowerKey(), higherKey()都是O(lgN)
Stream
- toArray(int[][]::new)
addNum: O(lgN) 做了好几次lgN: containsKey, lowerKey, higherKey, get()都是lgN
getInterval: O(N) 取决于最后有多少个interval
class SummaryRanges {
TreeMap<Integer, int[]> map;
/** Initialize your data structure here. */
public SummaryRanges() {
map = new TreeMap<>();
}
public void addNum(int val) {
if (map.containsKey(val)) return;
Integer prevKey = map.lowerKey(val);
Integer nextKey = map.higherKey(val);
if (prevKey != null && map.get(prevKey)[1] + 1 == val
&& nextKey != null && map.get(nextKey)[0] - 1 == val) {
map.get(prevKey)[1] = map.get(nextKey)[1];
map.remove(nextKey);
} else if (prevKey != null && map.get(prevKey)[1] + 1 == val) {
map.get(prevKey)[1] = val;
} else if (nextKey != null && map.get(nextKey)[0] - 1 == val) {
map.put(val, new int[]{val, map.get(nextKey)[1]});
map.remove(nextKey);
} else if (prevKey != null && map.get(prevKey)[1] >= val){
return;
} else {
map.put(val, new int[]{val, val});
}
}
public int[][] getIntervals() {
return map.entrySet()
.stream()
.map(entry -> new int[]{entry.getValue()[0], entry.getValue()[1]})
.toArray(int[][]::new);
}
}
/**
* Your SummaryRanges object will be instantiated and called as such:
* SummaryRanges obj = new SummaryRanges();
* obj.addNum(val);
* int[][] param_2 = obj.getIntervals();
*/
Follow up: What if there are lots of merges and the number of disjoint intervals are small compared to the data stream's size?
lots of merges说得很隐晦,其实指的是,很多次的进行add() -> 需要addNum()快一些。
log(n)已经很快了。。不知道怎么提速
然后最后interval少, getInterval()的 O(N)里N代表多少个区间,也很小,正好。。
(FB) 脸家给的follow-up是,addInterval(),不再是[2,2]这种,可以是[2,6]
楞做的话就 treeMap按照start排序
分情况
- 完全包含在现有区间->无视
- 完全不包含->加入新区间
- 连接前后2个区间 -> merge
- 和前后其中1个包含 -> 前后merge
- 包含好几个现有区间 -> 最小start,最大end
仔细想想也挺麻烦。。
352. Data Stream as Disjoint Intervals的更多相关文章
- [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 (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 ...
- 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 ...
- [leetcode]352. Data Stream as Disjoint Intervals
数据流合并成区间,每次新来一个数,表示成一个区间,然后在已经保存的区间中进行二分查找,最后结果有3种,插入头部,尾部,中间,插入头部,不管插入哪里,都判断一下左边和右边是否能和当前的数字接起来,我这样 ...
- 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 ...
随机推荐
- AS3.0的int uint Number的使用原则
int uint Number的使用原则: 1.能用整数值时优先使用:int uint 2.整数值有正负时使用:int 3.只处理正整数时使用:uint 4.处理好和颜色相关的值时使用:uint 5. ...
- php脚本业务逻辑
设置时区 设置执行不超时 设置根目录常量 引入配置文件(自定义/模板) 错误记录 定义业务类 执行业务类::run() 数据库单例初始化(连接) 日志单例初始化(引入日志类,配置日志路径,日志开关) ...
- Day18 Django之路由系统、模板语言、Ajax、Model
一.路由系统 1.创建Django项目 django-admin startproject day18 cd day18 python3 manage.py startapp app01 2.app0 ...
- iOS: 属性列表介绍 Introduction to Property Lists
iOS: 属性列表介绍 Introduction to Property Lists 从本质上说, 属性列表就是苹果的对象数据序列化与反序列化方式 属性列表使用几种数据类型把数据组织为键值表和值表 P ...
- EFBaseDal新增删除方法
public T Delete(int id ) { var entity = db.Set<T>().Find(id); T t ...
- Learning LexRank——Graph-based Centrality as Salience in Text Summarization(一)
(1)What is Sentence Centrality and Centroid-based Summarization ? Extractive summarization works by ...
- 在debug模式下运行不报错,换到release模式下报找不到某某库或文件的错。。解决办法
我遇到的问题是:把edit secheme调到debug模式运行没有问题,然后调到release模式的时候报目录下没有libTuyoo.a 解决办法 把断开真机设备,用IOS device下relea ...
- Gems
zoj2332:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2332 题意:这一道题的题意,我看了很久,也没有看明白,最终 ...
- 如何得到UBUNTU源代码
http://www.pleaseguide.me/367/how-to-get-the-source-code-of-ubuntu 在按书作测试,有难点一一解决. Ubuntu's Source c ...
- Java System类看到的一点小记
System类 位置java.lang包中 是final类,不能被继承,不能被修改 ,不能被实例化 private System(){}私有的构造函数,不允许被其他对象进行实例化 public fin ...