LC710 - Random Pick With Blacklist

Description

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.

Solution

There are N numbers in total, of which there are len(blacklist) numbers on the blacklist, and N-len(blacklist) numbers on the whitelist. In order to use random.randint(0, self.white_len - 1) to get white_len numbers randomly One of the list numbers, using the mapping to handle numbers in the first white_len positions that are not part of the whitelist.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# Solution O(m) time, Pick O(1) time | O(m) space
class Solution:

def __init__(self, n: int, blacklist: List[int]):
self.wl = n - len(blacklist)
blacks = [i for i in blacklist if i < self.wl]
whites = [i for i in range(self.wl, n) if i not in blacklist]
self.blackwhite = {}
for i in range(len(blacks)):
self.blackwhite[blacks[i]] = whites[i]

def pick(self) -> int:
num = random.randint(0, self.wl-1)
if num in self.blackwhite:
return self.blackwhite[num]
else:
return num

# Your Solution object will be instantiated and called as such:
# obj = Solution(n, blacklist)
# param_1 = obj.pick()

Version2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class Solution:
def __init__(self, n: int, blacklist: List[int]):
self.bound = n - len(blacklist)
blacks = [i for i in blacklist if i >= self.bound]
self.pairs = {}
# idx is the starting point of the latter list to filter out white items
idx = self.bound
for i in blacklist:
if i < self.bound:
while idx in blacks:
idx +=1
self.pairs[i] = idx
idx+=1

def pick(self) -> int:
num = random.randint(0, self.bound-1)
if num in self.pairs:
return self.pairs[num]
else:
return num


# Your Solution object will be instantiated and called as such:
# obj = Solution(n, blacklist)
# param_1 = obj.pick()