-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataStreamDisjointIntervals.cpp
80 lines (74 loc) · 2.33 KB
/
dataStreamDisjointIntervals.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/* Coding challenge #352 from LeetCode */
/**
* 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:
/** Initialize your data structure here. */
SummaryRanges() {
}
void addNum(int val) {
bool inserted = false;
for (auto it = hashset.begin(); it != hashset.end(); ++it) {
if ((*it).second == val-1) {
int left = (*it).first;
hashset.erase(it);
hashset.emplace(std::make_pair(left,val));
inserted = true;
break;
}
if ((*it).first <= val && (*it).second >= val) {
inserted = true;
break;
}
}
for (auto it = hashset.begin(); it != hashset.end(); ++it) {
if ((*it).first == val+1) {
int right = (*it).second;
hashset.erase(it);
hashset.emplace(std::make_pair(val,right));
inserted = true;
break;
}
}
if (!inserted) {
hashset.emplace(std::make_pair(val,val));
}
if (hashset.size() > 1) {
auto it_prev = hashset.begin();
auto it_next = it_prev++;
for (; it_next != hashset.end(); ++it_next) {
if ((*it_prev).second == (*it_next).first) { // merge
int left = (*it_prev).first;
int right = (*it_next).second;
hashset.erase(it_prev);
hashset.erase(it_next);
hashset.emplace(std::make_pair(left,right));
break;
}
it_prev = it_next;
}
}
}
vector<Interval> getIntervals() {
std::vector<Interval> result;
for (auto elem : hashset) {
result.emplace_back(std::move(Interval(elem.first,elem.second)));
}
return result;
}
private:
std::set<std::pair<int,int>> hashset;
};
/**
* Your SummaryRanges object will be instantiated and called as such:
* SummaryRanges obj = new SummaryRanges();
* obj.addNum(val);
* vector<Interval> param_2 = obj.getIntervals();
*/