Class SmallestInfiniteSet
java.lang.Object
g2301_2400.s2336_smallest_number_in_infinite_set.SmallestInfiniteSet
2336 - Smallest Number in Infinite Set.<p>Medium</p>
<p>You have a set which contains all positive integers <code>[1, 2, 3, 4, 5, ...]</code>.</p>
<p>Implement the <code>SmallestInfiniteSet</code> class:</p>
<ul>
<li><code>SmallestInfiniteSet()</code> Initializes the <strong>SmallestInfiniteSet</strong> object to contain <strong>all</strong> positive integers.</li>
<li><code>int popSmallest()</code> <strong>Removes</strong> and returns the smallest integer contained in the infinite set.</li>
<li><code>void addBack(int num)</code> <strong>Adds</strong> a positive integer <code>num</code> back into the infinite set, if it is <strong>not</strong> already in the infinite set.</li>
</ul>
<p><strong>Example 1:</strong></p>
<p><strong>Input</strong></p>
<p>[“SmallestInfiniteSet”, “addBack”, “popSmallest”, “popSmallest”, “popSmallest”, “addBack”, “popSmallest”, “popSmallest”, “popSmallest”]</p>
<p>[ [], [2], [], [], [], [1], [], [], []]</p>
<p><strong>Output:</strong></p>
<p>[null, null, 1, 2, 3, null, 1, 4, 5]</p>
<p><strong>Explanation:</strong></p>
<pre><code> SmallestInfiniteSet smallestInfiniteSet = new SmallestInfiniteSet();
smallestInfiniteSet.addBack(2); // 2 is already in the set, so no change is made.
smallestInfiniteSet.popSmallest(); // return 1, since 1 is the smallest number, and remove it from the set.
smallestInfiniteSet.popSmallest(); // return 2, and remove it from the set.
smallestInfiniteSet.popSmallest(); // return 3, and remove it from the set.
smallestInfiniteSet.addBack(1); // 1 is added back to the set.
smallestInfiniteSet.popSmallest(); // return 1, since 1 was added back to the set and
// is the smallest number, and remove it from the set.
smallestInfiniteSet.popSmallest(); // return 4, and remove it from the set.
smallestInfiniteSet.popSmallest(); // return 5, and remove it from the set.
</code></pre>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= num <= 1000</code></li>
<li>At most <code>1000</code> calls will be made <strong>in total</strong> to <code>popSmallest</code> and <code>addBack</code>.</li>
</ul>
-
Constructor Summary
Constructors -
Method Summary
-
Constructor Details
-
SmallestInfiniteSet
public SmallestInfiniteSet()
-
-
Method Details
-
popSmallest
public int popSmallest() -
addBack
public void addBack(int num)
-