We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent b2728dc commit 83ac43cCopy full SHA for 83ac43c
C++/smallest-number-in-infinite-set.cpp
@@ -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