Smallest Number in Infinite Set LeetCode Solution

Problem – Smallest Number in Infinite Set LeetCode Solution

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

Implement the SmallestInfiniteSet class:

  • SmallestInfiniteSet() Initializes the SmallestInfiniteSet object to contain all positive integers.
  • int popSmallest() Removes and returns the smallest integer contained in the infinite set.
  • void addBack(int num) Adds a positive integer num back into the infinite set, if it is not already in the infinite set.

Example 1:

Input
["SmallestInfiniteSet", "addBack", "popSmallest", "popSmallest", "popSmallest", "addBack", "popSmallest", "popSmallest", "popSmallest"]
[[], [2], [], [], [], [1], [], [], []]
Output
[null, null, 1, 2, 3, null, 1, 4, 5]

Explanation
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.

Constraints:

  • 1 <= num <= 1000
  • At most 1000 calls will be made in total to popSmallest and addBack.

Smallest Number in Infinite Set LeetCode Solution in C++

class SmallestInfiniteSet {
public:
    set<int>st;
    SmallestInfiniteSet() {
        st.clear();
        for(int i=1;i<=1000;i++){
            st.insert(i);
        }
    }
    
    int popSmallest() {
        int t = *st.begin();
        st.erase(st.begin());
        return t;
    }
    
    void addBack(int num) {
        st.insert(num);
    }
};

Smallest Number in Infinite Set LeetCode Solution in Java

class SmallestInfiniteSet {
    SortedSet<Integer> s=new TreeSet<Integer>();
    public SmallestInfiniteSet() {
        for(int i=1;i<1001;i++){
            s.add(i);
        }
    }
    
    public int popSmallest() {
        int k=s.first();
        s.remove(k);
        return k;
    }
    
    public void addBack(int num) {
        s.add(num);
    }
}

Smallest Number in Infinite Set LeetCode Solution in Python

class SmallestInfiniteSet:

    def __init__(self):
        self.visited = set(x for x in range(1,1001))

    def popSmallest(self) -> int:
        temp = min(self.visited)
        self.visited.remove(temp)
        return temp

    def addBack(self, num: int) -> None:
        if num not in self.visited:
            self.visited.add(num)
Smallest Number in Infinite Set LeetCode Solution Review:

In our experience, we suggest you solve this Smallest Number in Infinite Set LeetCode Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Smallest Number in Infinite Set LeetCode Solution

Find on LeetCode

Conclusion:

I hope this Smallest Number in Infinite Set LeetCode Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions >>

LeetCode Solutions

Hacker Rank Solutions

CodeChef Solutions

Leave a Reply

Your email address will not be published.