Skip to content

Commit 83ac43c

Browse files
authored
Create smallest-number-in-infinite-set.cpp
1 parent b2728dc commit 83ac43c

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Time: ctor: O(1)
2+
// popSmallest: O(logn)
3+
// addBack: O(logn)
4+
// Space: O(n)
5+
6+
// bst
7+
class SmallestInfiniteSet {
8+
public:
9+
SmallestInfiniteSet() {
10+
11+
}
12+
13+
int popSmallest() {
14+
if (!empty(bst_)) {
15+
const int result = *cbegin(bst_);
16+
bst_.erase(result);
17+
return result;
18+
}
19+
return n_++;
20+
}
21+
22+
void addBack(int num) {
23+
if (num >= n_ || bst_.count(num)) {
24+
return;
25+
}
26+
bst_.emplace(num);
27+
}
28+
29+
private:
30+
int n_ = 1;
31+
set<int> bst_;
32+
};

0 commit comments

Comments
 (0)