mirror of
https://gitlab.com/mfocko/LeetCode.git
synced 2024-11-09 15:59:06 +01:00
cs: add “2336. Smallest Number in Infinite Set”
Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
parent
4cca12685d
commit
37e383c221
1 changed files with 35 additions and 0 deletions
35
cs/smallest-number-in-infinite-set.cs
Normal file
35
cs/smallest-number-in-infinite-set.cs
Normal file
|
@ -0,0 +1,35 @@
|
||||||
|
public class SmallestInfiniteSet {
|
||||||
|
private SortedSet<int> smaller;
|
||||||
|
private int nextInfinite;
|
||||||
|
|
||||||
|
public SmallestInfiniteSet() {
|
||||||
|
smaller = new SortedSet<int>();
|
||||||
|
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);
|
||||||
|
*/
|
Loading…
Reference in a new issue