mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-01-10 18:48:13 +08:00
41 lines
2.1 KiB
HTML
41 lines
2.1 KiB
HTML
<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> </p>
|
|
<p><strong>Example 1:</strong></p>
|
|
|
|
<pre>
|
|
<strong>Input</strong>
|
|
["SmallestInfiniteSet", "addBack", "popSmallest", "popSmallest", "popSmallest", "addBack", "popSmallest", "popSmallest", "popSmallest"]
|
|
[[], [2], [], [], [], [1], [], [], []]
|
|
<strong>Output</strong>
|
|
[null, null, 1, 2, 3, null, 1, 4, 5]
|
|
|
|
<strong>Explanation</strong>
|
|
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.
|
|
</pre>
|
|
|
|
<p> </p>
|
|
<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>
|