LeetCode题解——Data Stream as Disjoint Intervals

原创 2016年06月02日 09:07:12

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]


struct Interval {
     int start;
     int end;
     Interval() : start(0), end(0) {}
     Interval(int s, int e) : start(s), end(e) {}
 };

 
class SummaryRanges {
public:
    void addNum(int val) {
	auto Cmp = [](Interval a, Interval b) { return a.start < b.start; };
        auto it = lower_bound(vec.begin(), vec.end(), Interval(val, val), Cmp);
        int start = val, end = val;
        if(it != vec.begin() && (it-1)->end+1 >= val) it--;
        while(it != vec.end() && val+1 >= it->start && val-1 <= it->end)
        {
            start = min(start, it->start);
            end = max(end, it->end);
            it = vec.erase(it);
        }
        vec.insert(it,Interval(start, end));
    }

    vector<Interval> getIntervals() {
        return vec;
    }
private:
    vector<Interval> vec;
};

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

LeetCode 352. Data Stream as Disjoint Intervals(数据流区间)

原题网址:https://leetcode.com/problems/data-stream-as-disjoint-intervals/ Given a data stream input...
  • jmspan
  • jmspan
  • 2016年06月16日 01:11
  • 923

[leetcode] 352. Data Stream as Disjoint Intervals 解题报告

题目链接: https://leetcode.com/problems/data-stream-as-disjoint-intervals/ Given a data stream input o...

352. Data Stream as Disjoint Intervals

352. Data Stream as Disjoint Intervals

LeetCode295——Find Median from Data Stream

LeetCode295——Find Median from Data Stream 前提是Ordered List。 但是插入即addNum操作可能是无序的。 尝试过对序列排序再返回的办法结果是...

[leetcode] 295. Find Median from Data Stream

Median is the middle value in an ordered integer list. If the size of the list is even, there is no ...

leetcode 295 : Find Median from Data Stream

1、原题如下: Median is the middle value in an ordered integer list. If the size of the list is even, the...

LeetCode Find Median from Data Stream

Description: Median is the middle value in an ordered integer list. If the size of the list is ...

Leetcode 295. Find Median from Data Stream (Hard) (cpp)

Leetcode 295. Find Median from Data Stream (Hard) (cpp)
  • Niko_Ke
  • Niko_Ke
  • 2016年12月14日 00:54
  • 192

leetcode -- Find Median from Data Stream -- 设计题重点

https://leetcode.com/problems/find-median-from-data-stream/利用minHeap以及maxHeap。把一个ordered num list, 分...
  • xyqzki
  • xyqzki
  • 2015年12月25日 10:58
  • 298

[leetcode] 295. Find Median from Data Stream

题目:Median is the middle value in an ordered integer list. If the size of the list is even, there is ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:LeetCode题解——Data Stream as Disjoint Intervals
举报原因:
原因补充:

(最多只允许输入30个字)