diff --git a/cs/smallest-number-in-infinite-set.cs b/cs/smallest-number-in-infinite-set.cs new file mode 100644 index 0000000..fb4570c --- /dev/null +++ b/cs/smallest-number-in-infinite-set.cs @@ -0,0 +1,35 @@ +public class SmallestInfiniteSet { + private SortedSet smaller; + private int nextInfinite; + + public SmallestInfiniteSet() { + smaller = new SortedSet(); + nextInfinite = 1; + } + + public int PopSmallest() { + var m = smaller.Min; + if (m != 0) { + smaller.Remove(m); + return m; + } + + ++nextInfinite; + return nextInfinite - 1; + } + + public void AddBack(int num) { + if (num >= nextInfinite) { + return; + } + + smaller.Add(num); + } +} + +/** + * Your SmallestInfiniteSet object will be instantiated and called as such: + * SmallestInfiniteSet obj = new SmallestInfiniteSet(); + * int param_1 = obj.PopSmallest(); + * obj.AddBack(num); + */