710. Random Pick with Blacklist

710. Random Pick with Blacklist

Description

Difficulty: Hard

Related Topics: Array, Hash Table, Math, Binary Search, Sorting, Randomized

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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.

Hints/Notes

  • Use map

Solution

Language: C++

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
class Solution {
public:
    int size = 0;
    unordered_map<intint> nums;

    Solution(int n, vector<int>& blacklist) {
        size = n - blacklist.size();
// [0, 1, 2, 3, 4, 5, 6]
// nums.count(2 || 3 || 5) == true
        for (int blackNum : blacklist) {
            nums[blackNum] = blackNum;
        }

        int last = n - 1;
        for (int blackNum : blacklist) {
            if (blackNum >= size) {
                continue;
            }
            while (nums.count(last)) {
                last--;
            }
            nums[blackNum] = last--;
        }
// nums[2] = 6, nums[3] = 4
    }
    
    int pick() {
        int num = rand() % size;
        if (nums.count(num)) {
            return nums[num];
        } else {
            return num;
        }
    }
};

/**
 * Your Solution object will be instantiated and called as such:
 * Solution* obj = new Solution(n, blacklist);
 * int param_1 = obj->pick();
 */