Smallest Number in Infinite Set
You have an infinite set that initially contains all positive integers [1, 2, 3, 4, 5, ...].
Implement the SmallestInfiniteSet class:
- SmallestInfiniteSet(): Initializes the object to contain all positive integers.
- int popSmallest(): Removes and returns the strictly smallest integer currently contained in the infinite set.
- void addBack(int num): Adds a positive integer num back into the set, but only if it is not already present.
Note: For testing purposes, the input is provided as an array of commands strings and a 2D array of args integers representing the sequence of method calls.)
Example 1:
Input: commands = ["SmallestInfiniteSet", "addBack", "popSmallest", "popSmallest", "popSmallest"] args = [[], [2], [], [], []]
Output: [null, null, 1, 2, 3]
Explanation:
addBack(2) does nothing since 2 is already in the set. The next three popSmallest() calls return the smallest available integers: 1, 2, and 3.
Example 2:
Input: commands = ["SmallestInfiniteSet", "popSmallest", "addBack", "popSmallest"] args = [[], [], [10], []]
Output: [null, 1, null, 2]
Explanation:
1 is popped. addBack(10) does nothing because 10 was never popped. The next smallest available integer is 2.
Now Your Turn!
Pick the correct output for the given inputInput: commands = ["SmallestInfiniteSet", "popSmallest", "popSmallest", "addBack", "popSmallest"] args = [[], [], [], [1], []]
Still unsure what the problem is asking ?
Let’s go through a few more examples, step by step, to make it clearer.
Constraints:
- 1 <= num <= 1000
- At most 1000 calls will be made in total to popSmallest and addBack.