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 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?
Approach #1: C++.
/**
* Definition for an interval.
* struct Interval {
* int start;
* int end;
* Interval() : start(0), end(0) {}
* Interval(int s, int e) : start(s), end(e) {}
* };
*/
class SummaryRanges {
private:
vector<Interval> intervals = vector<Interval>(); int binarySearch(vector<Interval> intervals, int val) {
return binarySearchHelper(intervals, 0, intervals.size(), val);
} int binarySearchHelper(vector<Interval> intervals, int start, int end, int val) {
if (start == end) return -1;
if (start+1 == end && intervals[start].start < val) return start; int mid = (start + end) / 2; if (intervals[mid].start == val) return mid;
else if (intervals[mid].start < val)
return binarySearchHelper(intervals, mid, end, val);
else
return binarySearchHelper(intervals, start, mid, val);
} public:
/** Initialize your data structure here. */
SummaryRanges() { } void addNum(int val) {
int index = binarySearch(intervals, val);
if (index != -1 && intervals[index].end >= val)
return; if (index != intervals.size() - 1 && val + 1 == intervals[index+1].start)
intervals[index+1].start = val;
else if (index != -1 && val - 1 == intervals[index].end)
intervals[index].end = val;
else {
intervals.insert(intervals.begin()+index+1, Interval(val, val));
} if (index != -1 && intervals[index].end + 1 == intervals[index+1].start) {
intervals[index].end = intervals[index+1].end;
intervals.erase(intervals.begin()+index+1);
} return ;
} vector<Interval> getIntervals() {
return this->intervals;
}
}; /**
* Your SummaryRanges object will be instantiated and called as such:
* SummaryRanges obj = new SummaryRanges();
* obj.addNum(val);
* vector<Interval> param_2 = obj.getIntervals();
*/
Approach #2: Java.
/**
* Definition for an interval.
* public class Interval {
* int start;
* int end;
* Interval() { start = 0; end = 0; }
* Interval(int s, int e) { start = s; end = e; }
* }
*/
class SummaryRanges {
TreeMap<Integer, Interval> tree; /** Initialize your data structure here. */
public SummaryRanges() {
tree = new TreeMap<>();
} public void addNum(int val) {
if (tree.containsKey(val)) return ;
Integer l = tree.lowerKey(val);
Integer h = tree.higherKey(val);
if (l != null && h != null && tree.get(l).end + 1 == val && h == val + 1) {
tree.get(l).end = tree.get(h).end;
tree.remove(h);
} else if (l != null && tree.get(l).end + 1 >= val) {
tree.get(l).end = Math.max(tree.get(l).end, val);
} else if (h != null && h == val + 1) {
tree.put(val, new Interval(val, tree.get(h).end));
tree.remove(h);
} else {
tree.put(val, new Interval(val, val));
}
} public List<Interval> getIntervals() {
return new ArrayList<>(tree.values());
}
} /**
* Your SummaryRanges object will be instantiated and called as such:
* SummaryRanges obj = new SummaryRanges();
* obj.addNum(val);
* List<Interval> param_2 = obj.getIntervals();
*/
Appraoch #3: Python.
# Definition for an interval.
# class Interval(object):
# def __init__(self, s=0, e=0):
# self.start = s
# self.end = e class SummaryRanges(object): def __init__(self):
"""
Initialize your data structure here.
"""
self.intervals = [] def addNum(self, val):
"""
:type val: int
:rtype: void
"""
heapq.heappush(self.intervals, (val, Interval(val, val))) def getIntervals(self):
"""
:rtype: List[Interval]
"""
stack = []
while self.intervals:
idx, cur = heapq.heappop(self.intervals)
if not stack:
stack.append((idx, cur))
else:
_, prev = stack[-1]
if prev.end + 1 >= cur.start:
prev.end = max(prev.end, cur.end)
else:
stack.append((idx, cur))
self.intervals = stack
return list(map(lambda x : x[1], stack)) # Your SummaryRanges object will be instantiated and called as such:
# obj = SummaryRanges()
# obj.addNum(val)
# param_2 = obj.getIntervals()
Note:
Java -----> Treemap.
Class TreeMap<K,V>
- java.lang.Object
- java.util.AbstractMap<K,V>
- java.util.TreeMap<K,V>
- Type Parameters:
K- the type of keys maintained by this mapV- the type of mapped values
- All Implemented Interfaces:
- Serializable, Cloneable, Map<K,V>, NavigableMap<K,V>, SortedMap<K,V>
public class TreeMap<K,V>
extends AbstractMap<K,V>
implements NavigableMap<K,V>, Cloneable, SerializableA Red-Black tree basedNavigableMapimplementation. The map is sorted according to the natural ordering of its keys, or by aComparatorprovided at map creation time, depending on which constructor is used.This implementation provides guaranteed log(n) time cost for the
containsKey,get,putandremoveoperations. Algorithms are adaptations of those in Cormen, Leiserson, and Rivest's Introduction to Algorithms.Note that the ordering maintained by a tree map, like any sorted map, and whether or not an explicit comparator is provided, must be consistent with
equalsif this sorted map is to correctly implement theMapinterface. (SeeComparableorComparatorfor a precise definition of consistent with equals.) This is so because theMapinterface is defined in terms of theequalsoperation, but a sorted map performs all key comparisons using itscompareTo(orcompare) method, so two keys that are deemed equal by this method are, from the standpoint of the sorted map, equal. The behavior of a sorted map is well-defined even if its ordering is inconsistent withequals; it just fails to obey the general contract of theMapinterface.Note that this implementation is not synchronized. If multiple threads access a map concurrently, and at least one of the threads modifies the map structurally, it must be synchronized externally. (A structural modification is any operation that adds or deletes one or more mappings; merely changing the value associated with an existing key is not a structural modification.) This is typically accomplished by synchronizing on some object that naturally encapsulates the map. If no such object exists, the map should be "wrapped" using the
Collections.synchronizedSortedMapmethod. This is best done at creation time, to prevent accidental unsynchronized access to the map:SortedMap m = Collections.synchronizedSortedMap(new TreeMap(...));
The iterators returned by the
iteratormethod of the collections returned by all of this class's "collection view methods" are fail-fast: if the map is structurally modified at any time after the iterator is created, in any way except through the iterator's ownremovemethod, the iterator will throw aConcurrentModificationException. Thus, in the face of concurrent modification, the iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future.Note that the fail-fast behavior of an iterator cannot be guaranteed as it is, generally speaking, impossible to make any hard guarantees in the presence of unsynchronized concurrent modification. Fail-fast iterators throw
ConcurrentModificationExceptionon a best-effort basis. Therefore, it would be wrong to write a program that depended on this exception for its correctness: the fail-fast behavior of iterators should be used only to detect bugs.All
Map.Entrypairs returned by methods in this class and its views represent snapshots of mappings at the time they were produced. They do not support theEntry.setValuemethod. (Note however that it is possible to change mappings in the associated map usingput.)
Python -----> lambda.
当我们在传入函数时,有些时候,不需要显式地定义函数,直接传入匿名函数更方便。
在Python中,对匿名函数提供了有限支持。还是以map()函数为例,计算f(x)=x2时,除了定义一个f(x)的函数外,还可以直接传入匿名函数:
>>> list(map(lambda x: x * x, [1, 2, 3, 4, 5, 6, 7, 8, 9]))
[1, 4, 9, 16, 25, 36, 49, 64, 81]
通过对比可以看出,匿名函数lambda x: x * x实际上就是:
def f(x):
return x * x
关键字lambda表示匿名函数,冒号前面的x表示函数参数。
匿名函数有个限制,就是只能有一个表达式,不用写return,返回值就是该表达式的结果。
用匿名函数有个好处,因为函数没有名字,不必担心函数名冲突。此外,匿名函数也是一个函数对象,也可以把匿名函数赋值给一个变量,再利用变量来调用该函数:
>>> f = lambda x: x * x
>>> f
<function <lambda> at 0x101c6ef28>
>>> f(5)
25
同样,也可以把匿名函数作为返回值返回,比如:
def build(x, y):
return lambda: x * x + y * y
Python -----> heapq.
This module provides an implementation of the heap queue algorithm, also known as the priority queue algorithm.
Heaps are arrays for which heap[k] <= heap[2*k+1] and heap[k] <= heap[2*k+2] for all k, counting elements from zero. For the sake of comparison, non-existing elements are considered to be infinite. The interesting property of a heap is that heap[0] is always its smallest element.
The API below differs from textbook heap algorithms in two aspects: (a) We use zero-based indexing. This makes the relationship between the index for a node and the indexes for its children slightly less obvious, but is more suitable since Python uses zero-based indexing. (b) Our pop method returns the smallest item, not the largest (called a “min heap” in textbooks; a “max heap” is more common in texts because of its suitability for in-place sorting).
These two make it possible to view the heap as a regular Python list without surprises: heap[0] is the smallest item, and heap.sort() maintains the heap invariant!
To create a heap, use a list initialized to [], or you can transform a populated list into a heap via function heapify().
352. Data Stream as Disjoint Intervals (TreeMap, lambda, heapq)的更多相关文章
- 352. Data Stream as Disjoint Intervals
Plz take my miserable life T T. 和57 insert interval一样的,只不过insert好多. 可以直接用57的做法一个一个加,然后如果数据大的话,要用tree ...
- [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 ...
- [leetcode]352. Data Stream as Disjoint Intervals
数据流合并成区间,每次新来一个数,表示成一个区间,然后在已经保存的区间中进行二分查找,最后结果有3种,插入头部,尾部,中间,插入头部,不管插入哪里,都判断一下左边和右边是否能和当前的数字接起来,我这样 ...
- 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 ...
- 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 ...
- 352. Data Stream as Disjoint Interval
Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers seen ...
随机推荐
- CrystalReport runtime的下载地址
SAP网站的东西实在太多了,找个CrytalReport都费劲.13.*版的可以通过下面的地址下载: SAP Crystal Reports, developer version for Micros ...
- 关于Spring注解 @Service @Component @Controller @Repository 用法
@Component 相当于实例化类的对象,其他三个注解可以理解为@Component的子注解或细化. 在annotaion配置注解中用@Component来表示一个通用注释用于说明一个类是一个spr ...
- Android OpenGL 播放视频学习
1, 初步接触Open GL: http://www.cnblogs.com/TerryBlog/archive/2010/07/09/1774475.html 使用GLSurfaceView和Ren ...
- Java多线程系列 基础篇06 synchronized(同步锁)
转载 http://www.cnblogs.com/paddix/ 作者:liuxiaopeng http://www.infoq.com/cn/articles/java-se-16-synchro ...
- 使用valgrind进行内存泄漏和非法内存操作检测
valgrind是一个强大的工具,最常用的功能是用它来检测内存泄漏和非法内存的使用.要想让valgrind报告的更加细致,请使用-g进行编译. 基本命令如下: $ valgrind --tool=me ...
- 《C prime plus (第五版)》 ---第11章 字符串和字符串函数---3
字符串函数总结: 下面是头文件 string.h 中定义的函数: 序号 函数 & 描述 1 void *memchr(const void *str, int c, size_t n)在参数 ...
- Spring Boot2.0之 监控管理
Spring boot监控中心: 针对微服务的服务状态,服务器的内存变化(内存.线程.日志管理等)检测服务配置连接地址是否有用(有些懒加载的情况下,用的时候发现卧槽不能用)模拟访问,懒加载.统计有多少 ...
- Hadoop- MapReduce分布式计算框架原理
分布式计算: 原则:移动计算而尽可能减少移动数据(减少网络开销) 分布式计算其实就是将单台机器上的计算拓展到多台机器上并行计算. MapReduce是一种编程模型.Hadoop MapReduce采用 ...
- js正则表达式(2)
找到以某个字符串开头的字符串 var myReg=/^(abc)/gim; 如果不加m,那么只找一行,而加了m可以找到每行中以该字符串开头的匹配文本. 如: abcsfsdfasd7890hklfah ...
- ivew组件的使用
iview的官网:https://www.iviewui.com/docs/guide/start 1.选择快速上手 2.安装 解压,cmd,cd进你解压后的文件,cnpm i 3.打包 npm ru ...