RainX_69

Smallest Number in Infinite Set | MUST DO | OA | TRICKY

Apr 26th, 2023
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.46 KB | Source Code | 0 0
  1. https://leetcode.com/problems/smallest-number-in-infinite-set/
  2.  
  3. You have a set which contains all positive integers [1, 2, 3, 4, 5, ...].
  4.  
  5. Implement the SmallestInfiniteSet class:
  6. SmallestInfiniteSet() Initializes the SmallestInfiniteSet object to contain all positive integers.
  7. int popSmallest() Removes and returns the smallest integer contained in the infinite set.
  8. void addBack(int num) Adds a positive integer num back into the infinite set, if it is not already in the infinite set.
  9.  
  10.  
  11. Example 1:
  12. Input
  13. ["SmallestInfiniteSet", "addBack", "popSmallest", "popSmallest", "popSmallest", "addBack", "popSmallest", "popSmallest", "popSmallest"]
  14. [[], [2], [], [], [], [1], [], [], []]
  15. Output
  16. [null, null, 1, 2, 3, null, 1, 4, 5]
  17.  
  18. Explanation
  19. SmallestInfiniteSet smallestInfiniteSet = new SmallestInfiniteSet();
  20. smallestInfiniteSet.addBack(2);    // 2 is already in the set, so no change is made.
  21. smallestInfiniteSet.popSmallest(); // return 1, since 1 is the smallest number, and remove it from the set.
  22. smallestInfiniteSet.popSmallest(); // return 2, and remove it from the set.
  23. smallestInfiniteSet.popSmallest(); // return 3, and remove it from the set.
  24. smallestInfiniteSet.addBack(1);    // 1 is added back to the set.
  25. smallestInfiniteSet.popSmallest(); // return 1, since 1 was added back to the set and
  26.                                    // is the smallest number, and remove it from the set.
  27. smallestInfiniteSet.popSmallest(); // return 4, and remove it from the set.
  28. smallestInfiniteSet.popSmallest(); // return 5, and remove it from the set.
  29.  
  30.  
  31. Constraints:
  32.  
  33. 1 <= num <= 1000
  34. At most 1000 calls will be made in total to popSmallest and addBack.
  35.  
  36.  
  37. ------------------------------------------------------------------------------------------------------------------------------------------
  38.  
  39. class SmallestInfiniteSet {
  40. public:
  41.     int val=1;
  42.     set<int> PQ;
  43.    
  44.     SmallestInfiniteSet() {        
  45.     }
  46.    
  47.     int popSmallest() {
  48.         if(PQ.empty() || *PQ.begin()>val){
  49.             int x=val;
  50.             val++;
  51.             return x;
  52.         }
  53.         int x=*PQ.begin();
  54.         PQ.erase(x);
  55.         return x;
  56.     }
  57.    
  58.     void addBack(int num) {
  59.         if(val>num){
  60.             PQ.insert(num);        
  61.         }
  62.     }
  63. };
  64.  
  65. /**
  66.  * Your SmallestInfiniteSet object will be instantiated and called as such:
  67.  * SmallestInfiniteSet* obj = new SmallestInfiniteSet();
  68.  * int param_1 = obj->popSmallest();
  69.  * obj->addBack(num);
  70.  */
  71.  
  72.  
  73.  
  74.  
  75.  
  76.  
  77.  
Advertisement
Add Comment
Please, Sign In to add comment