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

4111人阅读 评论(0)

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]


What if there are lots of merges and the number of disjoint intervals are small compared to the data stream's size?

/**
* 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 {
public:
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;
};

/**
* Your SummaryRanges object will be instantiated and called as such:
* SummaryRanges obj = new SummaryRanges();
* vector<Interval> param_2 = obj.getIntervals();
*/

class SummaryRanges {
public:
/** Initialize your data structure here. */
auto it = st.lower_bound(Interval(val, val));
int start = val, end = val;
if(it != st.begin() && (--it)->end+1 < val) it++;
while(it != st.end() && val+1 >= it->start && val-1 <= it->end)
{
start = min(start, it->start);
end = max(end, it->end);
it = st.erase(it);
}
st.insert(it,Interval(start, end));
}

vector<Interval> getIntervals() {
vector<Interval> result;
for(auto val: st) result.push_back(val);
return result;
}
private:
struct Cmp{
bool operator()(Interval a, Interval b) { return a.start < b.start; }
};
set<Interval, Cmp> st;
};

0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：350453次
• 积分：7630
• 等级：
• 排名：第2901名
• 原创：425篇
• 转载：0篇
• 译文：0篇
• 评论：25条
最新评论