Count Integers in Intervals LeetCode solution in C++

1
class CountIntervals {
2
set<pair<int, int>> st;
3
int cnt;
4

5
public:
6
CountIntervals() {
7
cnt = 0;
8
}
9

10
void add(int left, int right) {
11
auto it = st.upper_bound({left, INT_MIN});
12

13
if (it != st.begin() && (--it)->second < left) {
14
++it;
15
}
16

17
while (it != st.end() && it->first <= right) {
18
left = min(left, it->first);
19
right = max(right, it->second);
20

21
cnt -= (it->second - it->first + 1);
22
it = st.erase(it);
23
}
24

25
st.insert({left, right});
26
cnt += (right - left + 1);
27
}
28

29
int count() {
30
return cnt;
31
}
32
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0