-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSummaryRanges.cpp
More file actions
58 lines (51 loc) · 1.6 KB
/
SummaryRanges.cpp
File metadata and controls
58 lines (51 loc) · 1.6 KB
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
class SummaryRanges {
public:
void addNum(int val) {
if (m.count(val)) return;
const int lo = lowerKey(val), hi = higherKey(val);
// {lo, m[lo]} + val + {hi, m[hi]} = {lo, m[hi]}
if (lo >= 0 && hi >= 0 && m[lo] + 1 == val && val + 1 == hi) {
m[lo] = m[hi];
m.erase(hi);
// {lo, m[lo]} + val = {lo, val}
// (prevent adding duplicate entry by using '>=' instead of '==')
} else if (lo >= 0 && m[lo] + 1 >= val) {
m[lo] = max(m[lo], val);
} else if (hi >= 0 && val + 1 == hi) {
// val + {hi, map[hi]} = {val, map[hi]}
m[val] = m[hi];
m.erase(hi);
} else {
m[val] = val;
}
}
vector<vector<int>> getIntervals() {
vector<vector<int>> ans;
ans.reserve(m.size());
for (const auto &it : m)
ans.push_back({it.first, it.second});
return ans;
}
private:
map<int, int> m; // {start: end}
// maximum in m < key
int lowerKey(int key) {
const auto it = m.lower_bound(key); // minimum in m >= key
if (it == m.cbegin())
return -1;
return std::prev(it)->first;
}
// minimum in m > key
int higherKey(int key) {
const auto it = m.upper_bound(key); // minimum in m > key
if (it == m.cend())
return -1;
return it->first;
}
};
/**
* Your SummaryRanges object will be instantiated and called as such:
* SummaryRanges* obj = new SummaryRanges();
* obj->addNum(val);
* vector<vector<int>> param_2 = obj->getIntervals();
*/