{ "data": { "question": { "questionId": "2413", "questionFrontendId": "2336", "boundTopicId": null, "title": "Smallest Number in Infinite Set", "titleSlug": "smallest-number-in-infinite-set", "content": "

You have a set which contains all positive integers [1, 2, 3, 4, 5, ...].

\n\n

Implement the SmallestInfiniteSet class:

\n\n\n\n

 

\n

Example 1:

\n\n
\nInput\n["SmallestInfiniteSet", "addBack", "popSmallest", "popSmallest", "popSmallest", "addBack", "popSmallest", "popSmallest", "popSmallest"]\n[[], [2], [], [], [], [1], [], [], []]\nOutput\n[null, null, 1, 2, 3, null, 1, 4, 5]\n\nExplanation\nSmallestInfiniteSet smallestInfiniteSet = new SmallestInfiniteSet();\nsmallestInfiniteSet.addBack(2);    // 2 is already in the set, so no change is made.\nsmallestInfiniteSet.popSmallest(); // return 1, since 1 is the smallest number, and remove it from the set.\nsmallestInfiniteSet.popSmallest(); // return 2, and remove it from the set.\nsmallestInfiniteSet.popSmallest(); // return 3, and remove it from the set.\nsmallestInfiniteSet.addBack(1);    // 1 is added back to the set.\nsmallestInfiniteSet.popSmallest(); // return 1, since 1 was added back to the set and\n                                   // is the smallest number, and remove it from the set.\nsmallestInfiniteSet.popSmallest(); // return 4, and remove it from the set.\nsmallestInfiniteSet.popSmallest(); // return 5, and remove it from the set.\n
\n\n

 

\n

Constraints:

\n\n\n", "translatedTitle": null, "translatedContent": null, "isPaidOnly": false, "difficulty": "Medium", "likes": 1521, "dislikes": 163, "isLiked": null, "similarQuestions": "[{\"title\": \"First Missing Positive\", \"titleSlug\": \"first-missing-positive\", \"difficulty\": \"Hard\", \"translatedTitle\": null}]", "exampleTestcases": "[\"SmallestInfiniteSet\",\"addBack\",\"popSmallest\",\"popSmallest\",\"popSmallest\",\"addBack\",\"popSmallest\",\"popSmallest\",\"popSmallest\"]\n[[],[2],[],[],[],[1],[],[],[]]", "categoryTitle": "Algorithms", "contributors": [], "topicTags": [ { "name": "Hash Table", "slug": "hash-table", "translatedName": null, "__typename": "TopicTagNode" }, { "name": "Design", "slug": "design", "translatedName": null, "__typename": "TopicTagNode" }, { "name": "Heap (Priority Queue)", "slug": "heap-priority-queue", "translatedName": null, "__typename": "TopicTagNode" } ], "companyTagStats": null, "codeSnippets": [ { "lang": "C++", "langSlug": "cpp", "code": "class SmallestInfiniteSet {\npublic:\n SmallestInfiniteSet() {\n \n }\n \n int popSmallest() {\n \n }\n \n void addBack(int num) {\n \n }\n};\n\n/**\n * Your SmallestInfiniteSet object will be instantiated and called as such:\n * SmallestInfiniteSet* obj = new SmallestInfiniteSet();\n * int param_1 = obj->popSmallest();\n * obj->addBack(num);\n */", "__typename": "CodeSnippetNode" }, { "lang": "Java", "langSlug": "java", "code": "class SmallestInfiniteSet {\n\n public SmallestInfiniteSet() {\n \n }\n \n public int popSmallest() {\n \n }\n \n public void addBack(int num) {\n \n }\n}\n\n/**\n * Your SmallestInfiniteSet object will be instantiated and called as such:\n * SmallestInfiniteSet obj = new SmallestInfiniteSet();\n * int param_1 = obj.popSmallest();\n * obj.addBack(num);\n */", "__typename": "CodeSnippetNode" }, { "lang": "Python", "langSlug": "python", "code": "class SmallestInfiniteSet(object):\n\n def __init__(self):\n \n\n def popSmallest(self):\n \"\"\"\n :rtype: int\n \"\"\"\n \n\n def addBack(self, num):\n \"\"\"\n :type num: int\n :rtype: None\n \"\"\"\n \n\n\n# Your SmallestInfiniteSet object will be instantiated and called as such:\n# obj = SmallestInfiniteSet()\n# param_1 = obj.popSmallest()\n# obj.addBack(num)", "__typename": "CodeSnippetNode" }, { "lang": "Python3", "langSlug": "python3", "code": "class SmallestInfiniteSet:\n\n def __init__(self):\n \n\n def popSmallest(self) -> int:\n \n\n def addBack(self, num: int) -> None:\n \n\n\n# Your SmallestInfiniteSet object will be instantiated and called as such:\n# obj = SmallestInfiniteSet()\n# param_1 = obj.popSmallest()\n# obj.addBack(num)", "__typename": "CodeSnippetNode" }, { "lang": "C", "langSlug": "c", "code": "\n\n\ntypedef struct {\n \n} SmallestInfiniteSet;\n\n\nSmallestInfiniteSet* smallestInfiniteSetCreate() {\n \n}\n\nint smallestInfiniteSetPopSmallest(SmallestInfiniteSet* obj) {\n \n}\n\nvoid smallestInfiniteSetAddBack(SmallestInfiniteSet* obj, int num) {\n \n}\n\nvoid smallestInfiniteSetFree(SmallestInfiniteSet* obj) {\n \n}\n\n/**\n * Your SmallestInfiniteSet struct will be instantiated and called as such:\n * SmallestInfiniteSet* obj = smallestInfiniteSetCreate();\n * int param_1 = smallestInfiniteSetPopSmallest(obj);\n \n * smallestInfiniteSetAddBack(obj, num);\n \n * smallestInfiniteSetFree(obj);\n*/", "__typename": "CodeSnippetNode" }, { "lang": "C#", "langSlug": "csharp", "code": "public class SmallestInfiniteSet {\n\n public SmallestInfiniteSet() {\n \n }\n \n public int PopSmallest() {\n \n }\n \n public void AddBack(int num) {\n \n }\n}\n\n/**\n * Your SmallestInfiniteSet object will be instantiated and called as such:\n * SmallestInfiniteSet obj = new SmallestInfiniteSet();\n * int param_1 = obj.PopSmallest();\n * obj.AddBack(num);\n */", "__typename": "CodeSnippetNode" }, { "lang": "JavaScript", "langSlug": "javascript", "code": "\nvar SmallestInfiniteSet = function() {\n \n};\n\n/**\n * @return {number}\n */\nSmallestInfiniteSet.prototype.popSmallest = function() {\n \n};\n\n/** \n * @param {number} num\n * @return {void}\n */\nSmallestInfiniteSet.prototype.addBack = function(num) {\n \n};\n\n/** \n * Your SmallestInfiniteSet object will be instantiated and called as such:\n * var obj = new SmallestInfiniteSet()\n * var param_1 = obj.popSmallest()\n * obj.addBack(num)\n */", "__typename": "CodeSnippetNode" }, { "lang": "TypeScript", "langSlug": "typescript", "code": "class SmallestInfiniteSet {\n constructor() {\n \n }\n\n popSmallest(): number {\n \n }\n\n addBack(num: number): void {\n \n }\n}\n\n/**\n * Your SmallestInfiniteSet object will be instantiated and called as such:\n * var obj = new SmallestInfiniteSet()\n * var param_1 = obj.popSmallest()\n * obj.addBack(num)\n */", "__typename": "CodeSnippetNode" }, { "lang": "PHP", "langSlug": "php", "code": "class SmallestInfiniteSet {\n /**\n */\n function __construct() {\n \n }\n \n /**\n * @return Integer\n */\n function popSmallest() {\n \n }\n \n /**\n * @param Integer $num\n * @return NULL\n */\n function addBack($num) {\n \n }\n}\n\n/**\n * Your SmallestInfiniteSet object will be instantiated and called as such:\n * $obj = SmallestInfiniteSet();\n * $ret_1 = $obj->popSmallest();\n * $obj->addBack($num);\n */", "__typename": "CodeSnippetNode" }, { "lang": "Swift", "langSlug": "swift", "code": "\nclass SmallestInfiniteSet {\n\n init() {\n \n }\n \n func popSmallest() -> Int {\n \n }\n \n func addBack(_ num: Int) {\n \n }\n}\n\n/**\n * Your SmallestInfiniteSet object will be instantiated and called as such:\n * let obj = SmallestInfiniteSet()\n * let ret_1: Int = obj.popSmallest()\n * obj.addBack(num)\n */", "__typename": "CodeSnippetNode" }, { "lang": "Kotlin", "langSlug": "kotlin", "code": "class SmallestInfiniteSet() {\n\n fun popSmallest(): Int {\n \n }\n\n fun addBack(num: Int) {\n \n }\n\n}\n\n/**\n * Your SmallestInfiniteSet object will be instantiated and called as such:\n * var obj = SmallestInfiniteSet()\n * var param_1 = obj.popSmallest()\n * obj.addBack(num)\n */", "__typename": "CodeSnippetNode" }, { "lang": "Dart", "langSlug": "dart", "code": "class SmallestInfiniteSet {\n\n SmallestInfiniteSet() {\n \n }\n \n int popSmallest() {\n \n }\n \n void addBack(int num) {\n \n }\n}\n\n/**\n * Your SmallestInfiniteSet object will be instantiated and called as such:\n * SmallestInfiniteSet obj = SmallestInfiniteSet();\n * int param1 = obj.popSmallest();\n * obj.addBack(num);\n */", "__typename": "CodeSnippetNode" }, { "lang": "Go", "langSlug": "golang", "code": "type SmallestInfiniteSet struct {\n \n}\n\n\nfunc Constructor() SmallestInfiniteSet {\n \n}\n\n\nfunc (this *SmallestInfiniteSet) PopSmallest() int {\n \n}\n\n\nfunc (this *SmallestInfiniteSet) AddBack(num int) {\n \n}\n\n\n/**\n * Your SmallestInfiniteSet object will be instantiated and called as such:\n * obj := Constructor();\n * param_1 := obj.PopSmallest();\n * obj.AddBack(num);\n */", "__typename": "CodeSnippetNode" }, { "lang": "Ruby", "langSlug": "ruby", "code": "class SmallestInfiniteSet\n def initialize()\n \n end\n\n\n=begin\n :rtype: Integer\n=end\n def pop_smallest()\n \n end\n\n\n=begin\n :type num: Integer\n :rtype: Void\n=end\n def add_back(num)\n \n end\n\n\nend\n\n# Your SmallestInfiniteSet object will be instantiated and called as such:\n# obj = SmallestInfiniteSet.new()\n# param_1 = obj.pop_smallest()\n# obj.add_back(num)", "__typename": "CodeSnippetNode" }, { "lang": "Scala", "langSlug": "scala", "code": "class SmallestInfiniteSet() {\n\n def popSmallest(): Int = {\n \n }\n\n def addBack(num: Int) {\n \n }\n\n}\n\n/**\n * Your SmallestInfiniteSet object will be instantiated and called as such:\n * var obj = new SmallestInfiniteSet()\n * var param_1 = obj.popSmallest()\n * obj.addBack(num)\n */", "__typename": "CodeSnippetNode" }, { "lang": "Rust", "langSlug": "rust", "code": "struct SmallestInfiniteSet {\n\n}\n\n\n/** \n * `&self` means the method takes an immutable reference.\n * If you need a mutable reference, change it to `&mut self` instead.\n */\nimpl SmallestInfiniteSet {\n\n fn new() -> Self {\n \n }\n \n fn pop_smallest(&self) -> i32 {\n \n }\n \n fn add_back(&self, num: i32) {\n \n }\n}\n\n/**\n * Your SmallestInfiniteSet object will be instantiated and called as such:\n * let obj = SmallestInfiniteSet::new();\n * let ret_1: i32 = obj.pop_smallest();\n * obj.add_back(num);\n */", "__typename": "CodeSnippetNode" }, { "lang": "Racket", "langSlug": "racket", "code": "(define smallest-infinite-set%\n (class object%\n (super-new)\n \n (init-field)\n \n ; pop-smallest : -> exact-integer?\n (define/public (pop-smallest)\n )\n ; add-back : exact-integer? -> void?\n (define/public (add-back num)\n )))\n\n;; Your smallest-infinite-set% object will be instantiated and called as such:\n;; (define obj (new smallest-infinite-set%))\n;; (define param_1 (send obj pop-smallest))\n;; (send obj add-back num)", "__typename": "CodeSnippetNode" }, { "lang": "Erlang", "langSlug": "erlang", "code": "-spec smallest_infinite_set_init_() -> any().\nsmallest_infinite_set_init_() ->\n .\n\n-spec smallest_infinite_set_pop_smallest() -> integer().\nsmallest_infinite_set_pop_smallest() ->\n .\n\n-spec smallest_infinite_set_add_back(Num :: integer()) -> any().\nsmallest_infinite_set_add_back(Num) ->\n .\n\n\n%% Your functions will be called as such:\n%% smallest_infinite_set_init_(),\n%% Param_1 = smallest_infinite_set_pop_smallest(),\n%% smallest_infinite_set_add_back(Num),\n\n%% smallest_infinite_set_init_ will be called before every test case, in which you can do some necessary initializations.", "__typename": "CodeSnippetNode" }, { "lang": "Elixir", "langSlug": "elixir", "code": "defmodule SmallestInfiniteSet do\n @spec init_() :: any\n def init_() do\n \n end\n\n @spec pop_smallest() :: integer\n def pop_smallest() do\n \n end\n\n @spec add_back(num :: integer) :: any\n def add_back(num) do\n \n end\nend\n\n# Your functions will be called as such:\n# SmallestInfiniteSet.init_()\n# param_1 = SmallestInfiniteSet.pop_smallest()\n# SmallestInfiniteSet.add_back(num)\n\n# SmallestInfiniteSet.init_ will be called before every test case, in which you can do some necessary initializations.", "__typename": "CodeSnippetNode" } ], "stats": "{\"totalAccepted\": \"110.8K\", \"totalSubmission\": \"152.2K\", \"totalAcceptedRaw\": 110828, \"totalSubmissionRaw\": 152222, \"acRate\": \"72.8%\"}", "hints": [ "Based on the constraints, what is the maximum element that can possibly be popped?", "Maintain whether elements are in or not in the set. How many elements do we consider?" ], "solution": { "id": "1802", "canSeeDetail": false, "paidOnly": true, "hasVideoSolution": false, "paidOnlyVideo": true, "__typename": "ArticleNode" }, "status": null, "sampleTestCase": "[\"SmallestInfiniteSet\",\"addBack\",\"popSmallest\",\"popSmallest\",\"popSmallest\",\"addBack\",\"popSmallest\",\"popSmallest\",\"popSmallest\"]\n[[],[2],[],[],[],[1],[],[],[]]", "metaData": "{\n \"classname\": \"SmallestInfiniteSet\",\n \"constructor\": {\n \"params\": []\n },\n \"methods\": [\n {\n \"params\": [],\n \"name\": \"popSmallest\",\n \"return\": {\n \"type\": \"integer\"\n }\n },\n {\n \"params\": [\n {\n \"type\": \"integer\",\n \"name\": \"num\"\n }\n ],\n \"name\": \"addBack\",\n \"return\": {\n \"type\": \"void\"\n }\n }\n ],\n \"return\": {\n \"type\": \"boolean\"\n },\n \"systemdesign\": true\n}", "judgerAvailable": true, "judgeType": "large", "mysqlSchemas": [], "enableRunCode": true, "enableTestMode": false, "enableDebugger": true, "envInfo": "{\"cpp\": [\"C++\", \"

Compiled with clang 11 using the latest C++ 20 standard.

\\r\\n\\r\\n

Your code is compiled with level two optimization (-O2). AddressSanitizer is also enabled to help detect out-of-bounds and use-after-free bugs.

\\r\\n\\r\\n

Most standard library headers are already included automatically for your convenience.

\"], \"java\": [\"Java\", \"

OpenJDK 17. Java 8 features such as lambda expressions and stream API can be used.

\\r\\n\\r\\n

Most standard library headers are already included automatically for your convenience.

\\r\\n

Includes Pair class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.

\"], \"python\": [\"Python\", \"

Python 2.7.12.

\\r\\n\\r\\n

Most libraries are already imported automatically for your convenience, such as array, bisect, collections. If you need more libraries, you can import it yourself.

\\r\\n\\r\\n

For Map/TreeMap data structure, you may use sortedcontainers library.

\\r\\n\\r\\n

Note that Python 2.7 will not be maintained past 2020. For the latest Python, please choose Python3 instead.

\"], \"c\": [\"C\", \"

Compiled with gcc 8.2 using the gnu11 standard.

\\r\\n\\r\\n

Your code is compiled with level one optimization (-O1). AddressSanitizer is also enabled to help detect out-of-bounds and use-after-free bugs.

\\r\\n\\r\\n

Most standard library headers are already included automatically for your convenience.

\\r\\n\\r\\n

For hash table operations, you may use uthash. \\\"uthash.h\\\" is included by default. Below are some examples:

\\r\\n\\r\\n

1. Adding an item to a hash.\\r\\n

\\r\\nstruct hash_entry {\\r\\n    int id;            /* we'll use this field as the key */\\r\\n    char name[10];\\r\\n    UT_hash_handle hh; /* makes this structure hashable */\\r\\n};\\r\\n\\r\\nstruct hash_entry *users = NULL;\\r\\n\\r\\nvoid add_user(struct hash_entry *s) {\\r\\n    HASH_ADD_INT(users, id, s);\\r\\n}\\r\\n
\\r\\n

\\r\\n\\r\\n

2. Looking up an item in a hash:\\r\\n

\\r\\nstruct hash_entry *find_user(int user_id) {\\r\\n    struct hash_entry *s;\\r\\n    HASH_FIND_INT(users, &user_id, s);\\r\\n    return s;\\r\\n}\\r\\n
\\r\\n

\\r\\n\\r\\n

3. Deleting an item in a hash:\\r\\n

\\r\\nvoid delete_user(struct hash_entry *user) {\\r\\n    HASH_DEL(users, user);  \\r\\n}\\r\\n
\\r\\n

\"], \"csharp\": [\"C#\", \"

C# 10 with .NET 6 runtime

\"], \"javascript\": [\"JavaScript\", \"

Node.js 16.13.2.

\\r\\n\\r\\n

Your code is run with --harmony flag, enabling new ES6 features.

\\r\\n\\r\\n

lodash.js library is included by default.

\\r\\n\\r\\n

For Priority Queue / Queue data structures, you may use 5.3.0 version of datastructures-js/priority-queue and 4.2.1 version of datastructures-js/queue.

\"], \"ruby\": [\"Ruby\", \"

Ruby 3.1

\\r\\n\\r\\n

Some common data structure implementations are provided in the Algorithms module: https://www.rubydoc.info/github/kanwei/algorithms/Algorithms

\"], \"swift\": [\"Swift\", \"

Swift 5.5.2.

\"], \"golang\": [\"Go\", \"

Go 1.21

\\r\\n

Support https://godoc.org/github.com/emirpasic/gods@v1.18.1 library.

\"], \"python3\": [\"Python3\", \"

Python 3.10.

\\r\\n\\r\\n

Most libraries are already imported automatically for your convenience, such as array, bisect, collections. If you need more libraries, you can import it yourself.

\\r\\n\\r\\n

For Map/TreeMap data structure, you may use sortedcontainers library.

\"], \"scala\": [\"Scala\", \"

Scala 2.13.7.

\"], \"kotlin\": [\"Kotlin\", \"

Kotlin 1.9.0.

\"], \"rust\": [\"Rust\", \"

Rust 1.58.1

\\r\\n\\r\\n

Supports rand v0.6\\u00a0from crates.io

\"], \"php\": [\"PHP\", \"

PHP 8.1.

\\r\\n

With bcmath module

\"], \"typescript\": [\"Typescript\", \"

TypeScript 5.1.6, Node.js 16.13.2.

\\r\\n\\r\\n

Your code is run with --harmony flag, enabling new ES2022 features.

\\r\\n\\r\\n

lodash.js library is included by default.

\"], \"racket\": [\"Racket\", \"

Run with Racket 8.3.

\"], \"erlang\": [\"Erlang\", \"Erlang/OTP 25.0\"], \"elixir\": [\"Elixir\", \"Elixir 1.13.4 with Erlang/OTP 25.0\"], \"dart\": [\"Dart\", \"

Dart 2.17.3

\\r\\n\\r\\n

Your code will be run directly without compiling

\"]}", "libraryUrl": null, "adminUrl": null, "challengeQuestion": { "id": "1313", "date": "2023-04-25", "incompleteChallengeCount": 0, "streakCount": 0, "type": "DAILY", "__typename": "ChallengeQuestionNode" }, "__typename": "QuestionNode" } } }