forked from rui-yan/LeetCode-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data-stream-as-disjoint-intervals.cpp
165 lines (140 loc) · 4.06 KB
/
data-stream-as-disjoint-intervals.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// Time: addNum: O(logn), getIntervals: O(n), n is the number of disjoint intervals.
// Space: O(n)
/**
* Definition for an interval.
* struct Interval {
* int start;
* int end;
* Interval() : start(0), end(0) {}
* Interval(int s, int e) : start(s), end(e) {}
* };
*/
// Using set.
class SummaryRanges {
public:
/** Initialize your data structure here. */
SummaryRanges() {
}
void addNum(int val) {
auto it = intervals_.upper_bound(Interval(val, val));
int start = val, end = val;
if (it != intervals_.begin() && prev(it)->end + 1 >= val) {
--it;
}
while (it != intervals_.end() && end + 1 >= it->start) {
start = min(start, it->start);
end = max(end, it->end);
it = intervals_.erase(it);
}
intervals_.insert(it, Interval(start, end));
}
vector<Interval> getIntervals() {
return {intervals_.cbegin(), intervals_.cend()};
}
private:
struct Compare {
bool operator() (const Interval& a, const Interval& b) {
return a.start < b.start;
}
};
set<Interval, Compare> intervals_;
};
// Using map.
class SummaryRanges {
public:
/** Initialize your data structure here. */
SummaryRanges() {
}
void addNum(int val) {
auto it = intervals_.upper_bound(val);
int start = val, end = val;
if (it != intervals_.begin() && prev(it)->second + 1 >= val) {
--it;
}
while (it != intervals_.end() && end + 1 >= it->first) {
start = min(start, it->first);
end = max(end, it->second);
it = intervals_.erase(it);
}
intervals_[start] = end;
}
vector<Interval> getIntervals() {
vector<Interval> result;
for (const auto& kvp : intervals_) {
result.emplace_back(kvp.first, kvp.second);
}
return result;
}
private:
map<int, int> intervals_;
};
// Time: addNum: O(n), getIntervals: O(n), n is the number of disjoint intervals.
// Space: O(n)
class SummaryRanges2 {
public:
public:
/** Initialize your data structure here. */
SummaryRanges2() {
}
void addNum(int val) {
auto it = upper_bound(intervals_.begin(), intervals_.end(), Interval(val, val), Compare());
int start = val, end = val;
if (it != intervals_.begin() && prev(it)->end + 1 >= val) {
--it;
}
while (it != intervals_.end() && end + 1 >= it->start) {
start = min(start, it->start);
end = max(end, it->end);
it = intervals_.erase(it);
}
intervals_.insert(it, Interval(start, end));
}
vector<Interval> getIntervals() {
return intervals_;
}
private:
struct Compare {
bool operator() (const Interval& a, const Interval& b) {
return a.start < b.start;
}
};
vector<Interval> intervals_;
};
// Time: addNum: O(logs), getIntervals: O(s), s is the data stream's size.
// Space: O(s)
class SummaryRanges3 {
public:
/** Initialize your data structure here. */
SummaryRanges3() {
}
void addNum(int val) {
nums_.emplace(val);
}
vector<Interval> getIntervals() {
vector<Interval> result;
if (nums_.empty()) {
return result;
}
auto start = *nums_.begin(), end = *nums_.begin();
for (auto it = next(nums_.begin()); it != nums_.end(); ++it) {
if (it != nums_.end() && *it == end + 1) {
end = *it;
} else {
result.emplace_back(start, end);
if (it != nums_.end()) {
start = end = *it;
}
}
}
result.emplace_back(start, end);
return result;
}
private:
set<int> nums_;
};
/**
* Your SummaryRanges object will be instantiated and called as such:
* SummaryRanges obj = new SummaryRanges();
* obj.addNum(val);
* vector<Interval> param_2 = obj.getIntervals();
*/