Smallest Number in Infinite Set and Minimum Moves to Reach Target Score
Imagine you have a collection that initially contains every positive integer starting from 1 and going upwards indefinitely, like [1, 2, 3, 4, 5, ...]
.
Your task is to create a class called MinInfiniteSet
that supports the following operations:
MinInfiniteSet()
: Initializes the object so that it contains all positive integers.int getMin()
: Removes and returns the smallest number currently present in the set.void restore(int val)
: Adds a positive integer val
back to the set, but only if it is not already present.MinInfiniteSet minSet = new MinInfiniteSet(); minSet.restore(3); // 3 is already in the set, so no changes made. minSet.getMin(); // Returns 1 and removes it from the set. minSet.getMin(); // Returns 2 and removes it. minSet.getMin(); // Returns 3 and removes it. minSet.restore(1); // 1 is added back to the set. minSet.getMin(); // Returns 1 since it was restored. minSet.getMin(); // Returns 4 and removes it. minSet.getMin(); // Returns 5 and removes it.
getMin()
and restore()
.