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的更多相关文章

  1. [LeetCode] 352. Data Stream as Disjoint Intervals 分离区间的数据流

    Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers seen ...

  2. 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 ...

  3. 【leetcode】352. Data Stream as Disjoint Intervals

    问题描述: Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers ...

  4. 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 ...

  5. [leetcode]352. Data Stream as Disjoint Intervals

    数据流合并成区间,每次新来一个数,表示成一个区间,然后在已经保存的区间中进行二分查找,最后结果有3种,插入头部,尾部,中间,插入头部,不管插入哪里,都判断一下左边和右边是否能和当前的数字接起来,我这样 ...

  6. 352[LeetCode] Data Stream as Disjoint Intervals

    Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers seen ...

  7. [LeetCode] Data Stream as Disjoint Intervals 分离区间的数据流

    Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers seen ...

  8. 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 ...

  9. [Swift]LeetCode352. 将数据流变为多个不相交间隔 | Data Stream as Disjoint Intervals

    Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers seen ...

随机推荐

  1. 多重背包的入门题目HDU1171,2191,2844.

    首先,什么叫多重背包呢? 大概意思就是:一个背包有V总容量,有N种物品,其价值分别为Val1,Val2--,Val3,体积对应的是Vol1,Vol2,--,Vol3,件数对应Num1,Num2--,N ...

  2. BitMap(比特位)

    所谓的Bit-map就是用一个bit位来标记某个元素对应的Value, 而Key即是该元素.由于采用了Bit为单位来存储数据,因此在存储空间方面,可以大大节省. 腾讯面试的时候,让写了一个BitMap ...

  3. 【实习记】2014-08-23网络安全XSS与CSRF总结

        XSS:脚本中的不速之客XSS:跨站脚本(Cross-site scripting)CSRF:冒充用户之手CSRF:跨站请求伪造(Cross-site request forgery) 谷歌搜 ...

  4. ubuntu 之旅 —— eclipse没有菜单栏

    1. 新建一个eclipse.sh文件,加入如下内容,下面的路径是elcipse的路径 export UBUNTU_MENUPROXY=0 /home/wangdeshui/eclipse/eclip ...

  5. php递归数组中的应用

    <?php $arr = array(array(1,2), array(3, 4), array(array(5, 6), array(7, 8)));function t($a){    i ...

  6. 转:Ext GridPanel根据条件显示复选框

    Ext GridPanel实现复选框选择框: var selectModel = new Ext.grid.CheckboxSelectionModel({ singleSelect : false ...

  7. js 删除数组方法

    今天遇到一个比较脑残的问题 ,在在用js删除数组的时候 delete 数组[下标]的方法删除数组时,该数组的下标变为null,但是数组的长度并没有发生相应的变化 转而使用splice(小标,第N个)删 ...

  8. Python尾递归-求斐波那契数列

    # coding=utf-8 # Fibonacci.py Fib = {} def Fibonacci(n): global Fib if Fib.has_key(n): return Fib[n] ...

  9. 计算机视觉的matlab工具箱及MVG等

    MATLAB Functions for Multiple View Geometry Peter Kovesi's Matlab functions for Computer Vision Jean ...

  10. EQueue 2.3.2

    EQueue 2.3.2版本发布(支持高可用) 前言 前段时间针对EQueue的完善终于告一段落了,实在值得庆祝,自己的付出和坚持总算有了成果.这次新版本主要为EQueue实现了集群功能,基本实现了B ...