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. Graphics类绘制图形

    1. 画直线 void drawLine(int startX,int startY,int endX,int endY); 四个参数分别为:起始点的x坐标和y坐标以及终点的x坐标和y坐标,该方法用于 ...

  2. Ubuntu系列Crontab日记记录

    修改rsyslog文件,将/etc/rsyslog.d/50-default.conf 文件中的#cron.*前的#删掉 重启rsyslog服务service rsyslog restart 重启cr ...

  3. .NET生成静态页面例子

    主要做法如下: 1.创建网站,并创建一个模板页,template.htm 2.添加一个web窗体Default.aspx 3.在网站下新建文件夹htm,设置该文件夹的属性,确保该文件夹具有可写权限 详 ...

  4. ES 的CRUD 简单操作(小试牛刀)

    URL的格式: http://localhost:9200/<index>/<type>/[<id>] 其中index.type是必须提供的. id是可选的,不提供 ...

  5. APACHE 与IIS同时存在的情况下,给APACHE添加反向代理 共用80端口

    一.首先打开IIS,将IIS的端口改成81,不要让IIS占用了80端口 二.打开APACHE的httpd.conf配置文件,将里面的端口配置成80 三.打开APACHE的虚拟目录配置文件,如:http ...

  6. Git (1)

    对于程序员来说,版本管理工具可称得上是必需品.越来越多的版本管理开始使用Git,特别是Github的流行更加使得大家对于Git越来越关注.因此对于Git的学习是非常必要和重要的. 先看几个好玩的数据: ...

  7. javascript 学习笔记之JQuery中的Deferred对象

    Deffered是Jquery中的一个非常重要的对象,从1.5版本之后,Jquery中的ajax操作都基于Deffered进行了重构,这个对象的处理模式就像其他Javascript框中的Promise ...

  8. python运维开发之第二天

    一.模块初识: 1.模块定义 python是由一系列的模块组成的,每个模块就是一个py为后缀的文件,同时模块也是一个命名空间,从而避免了变量名称冲突的问题.模块我们就可以理解为lib库,如果需要使用某 ...

  9. Python: 使用zipfile+io模块在内存中进行zip操作

    #!/usr/bin/env python #coding=utf-8 ''' 版权所有 (c) 2014 yao_yu (http://blog.csdn.net/yao_yu_126) 本代码采用 ...

  10. Android Studio HelloWorld

    开发第一应用 可以开发属于自己的应用,是否有点小激动?好吧!让我们开始,首先点击Start a new Android Studio Project创建工程: 接下来需要输入应用名称(第一个字母要大写 ...