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 ...
随机推荐
- android入门到熟练(一)
1.andro系统架构:Linux内核层(提供驱动),系统运行库层和android运行时库(提供C/C++库的主要特性,如SQLite,OpenGL,Webkit等和Dalvik虚拟机),应用框架层, ...
- sql 命令操作用法
---恢复内容开始--- 远程登录数据库: mysql -u root -p 要求输入密码 ============== 查看数据库: show databases;============= 选择数 ...
- DOM 之 document 查找元素方法
DOM 之 document 查找元素方法 getElementById("idName"); // 始终取得第一个 idName 的元素 getElementsByTagName ...
- Memcache,Redis
Memcache Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态.数据库驱动网站的速度. ...
- JS实现继承多态
//类对象构造模版,无new访问,类似静态访问 var Class = { create: function () { return function () { //initialize初始化 //a ...
- Javascript异步编程的4种方法(转载)
原博地址: http://www.ruanyifeng.com/blog/2012/12/asynchronous%EF%BC%BFjavascript.html
- Python如何进行cross validation training
以4-fold validation training为例 (1) 给定数据集data和标签集label 样本个数为 sampNum = len(data) (2) 将给定的所有examples分为1 ...
- angularjs 资源集合
AngularJS是Google开源的一款JavaScript MVC框架,弥补了HTML在构建应用方面的不足. 源码托管在Github上,其通过使用指令(directives)结构来扩展HTML词汇 ...
- PHP 如何安全的使用 MySQL ?
大多数 PHP 程序员对 MySQL 肯定不陌生,至于各种 MySQL 函数的用法在开发手册和 w3school 这类网站上也有很多介绍.但是,你所用的写法真的安全吗?面对越来越猖獗的黑客攻击,SQL ...
- Python connect zookeeper use the kazoo module
doc:http://kazoo.readthedocs.org/en/latest/basic_usage.html eg: from kazoo.client import KazooClient ...