Advertisement
nikunjsoni

710

May 15th, 2021
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.70 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     int wlen;
  4.     unordered_map<int, int> m;
  5.    
  6.     Solution(int n, vector<int>& blacklist) {
  7.         wlen = n-blacklist.size();
  8.         unordered_set<int> white;
  9.         for(int i=wlen; i<n; i++) white.insert(i);
  10.         for(int bnum : blacklist) white.erase(bnum);
  11.         auto wi = white.begin();
  12.         for(int bnum: blacklist){
  13.             if(bnum < wlen)
  14.                 m[bnum] = *wi++;
  15.         }
  16.     }
  17.    
  18.     int pick() {
  19.         int k = rand() % wlen;
  20.         return m.count(k) ? m[k] : k;
  21.     }
  22. };
  23.  
  24. /**
  25.  * Your Solution object will be instantiated and called as such:
  26.  * Solution* obj = new Solution(n, blacklist);
  27.  * int param_1 = obj->pick();
  28.  */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement