You are given an integer n and an array of unique integers blacklist. Design an algorithm to pick a random integer in the range [0, n - 1] that is not in blacklist. Any integer that is in the mentioned range and not in blacklist should be equally likely to be returned.
Optimize your algorithm such that it minimizes the number of calls to the built-in random function of your language.
Implement the Solution class:
Solution(int n, int[] blacklist) Initializes the object with the integer n and the blacklisted integers blacklist.
int pick() Returns a random integer in the range [0, n - 1] and not in blacklist.
Example 1:
Input
["Solution", "pick", "pick", "pick", "pick", "pick", "pick", "pick"]
[[7, [2, 3, 5]], [], [], [], [], [], [], []]
Output
[null, 0, 4, 1, 6, 1, 0, 4]
Explanation
Solution solution = new Solution(7, [2, 3, 5]);
solution.pick(); // return 0, any integer from [0,1,4,6] should be ok. Note that for every call of pick,
// 0, 1, 4, and 6 must be equally likely to be returned (i.e., with probability 1/4).
solution.pick(); // return 4
solution.pick(); // return 1
solution.pick(); // return 6
solution.pick(); // return 1
solution.pick(); // return 0
solution.pick(); // return 4
Constraints:
1 <= n <= 109
0 <= blacklist.length <= min(105, n - 1)
0 <= blacklist[i] < n
All the values of blacklist are unique.
At most 2 * 104 calls will be made to pick.
Solutions
Solution 1
1 2 3 4 5 6 7 8 9101112131415161718192021
classSolution:def__init__(self,n:int,blacklist:List[int]):self.k=n-len(blacklist)self.d={}i=self.kblack=set(blacklist)forbinblacklist:ifb<self.k:whileiinblack:i+=1self.d[b]=ii+=1defpick(self)->int:x=randrange(self.k)returnself.d.get(x,x)# Your Solution object will be instantiated and called as such:# obj = Solution(n, blacklist)# param_1 = obj.pick()
classSolution{privateMap<Integer,Integer>d=newHashMap<>();privateRandomrand=newRandom();privateintk;publicSolution(intn,int[]blacklist){k=n-blacklist.length;inti=k;Set<Integer>black=newHashSet<>();for(intb:blacklist){black.add(b);}for(intb:blacklist){if(b<k){while(black.contains(i)){++i;}d.put(b,i++);}}}publicintpick(){intx=rand.nextInt(k);returnd.getOrDefault(x,x);}}/** * Your Solution object will be instantiated and called as such: * Solution obj = new Solution(n, blacklist); * int param_1 = obj.pick(); */
classSolution{public:unordered_map<int,int>d;intk;Solution(intn,vector<int>&blacklist){k=n-blacklist.size();inti=k;unordered_set<int>black(blacklist.begin(),blacklist.end());for(int&b:blacklist){if(b<k){while(black.count(i))++i;d[b]=i++;}}}intpick(){intx=rand()%k;returnd.count(x)?d[x]:x;}};/** * Your Solution object will be instantiated and called as such: * Solution* obj = new Solution(n, blacklist); * int param_1 = obj->pick(); */
typeSolutionstruct{dmap[int]intkint}funcConstructor(nint,blacklist[]int)Solution{k:=n-len(blacklist)i:=kblack:=map[int]bool{}for_,b:=rangeblacklist{black[b]=true}d:=map[int]int{}for_,b:=rangeblacklist{ifb<k{forblack[i]{i++}d[b]=ii++}}returnSolution{d,k}}func(this*Solution)Pick()int{x:=rand.Intn(this.k)ifv,ok:=this.d[x];ok{returnv}returnx}/** * Your Solution object will be instantiated and called as such: * obj := Constructor(n, blacklist); * param_1 := obj.Pick(); */