710. Random Pick with Blacklist
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 < = 1 0 9 1 <= n <= 10^9 1<=n<=109
- 0 < = b l a c k l i s t . l e n g t h < = m i n ( 1 0 5 , n − 1 ) 0 <= blacklist.length <= min(10^5, n - 1) 0<=blacklist.length<=min(105,n−1)
- 0 <= blacklist[i] < n
- All the values of blacklist are unique.
- At most 2 ∗ 1 0 4 2 * 10^4 2∗104 calls will be made to pick.
From: LeetCode
Link: 710. Random Pick with Blacklist
Solution:
Ideas:
-
Mapping only required for blacklisted elements < W — those are remapped to [W, n-1]
-
Hash table (map) stores mapping from blacklisted indices to non-blacklisted ones
-
Random number generation is limited to [0, W-1] — much smaller space
-
Memory-efficient even when n is large (e.g., 10⁹) because we don’t store all values
Code:
#define HASH_SIZE 131071 // A large prime number
typedef struct MapEntry {
int key;
int value;
struct MapEntry* next;
} MapEntry;
typedef struct {
int W;
MapEntry** map; // Hash table
} Solution;
// Hash function for integers
int hash(int key) {
return ((unsigned int)key) % HASH_SIZE;
}
void insert(MapEntry** map, int key, int value) {
int idx = hash(key);
MapEntry* newNode = (MapEntry*)malloc(sizeof(MapEntry));
newNode->key = key;
newNode->value = value;
newNode->next = map[idx];
map[idx] = newNode;
}
bool find(MapEntry** map, int key, int* value) {
int idx = hash(key);
MapEntry* node = map[idx];
while (node) {
if (node->key == key) {
if (value) *value = node->value;
return true;
}
node = node->next;
}
return false;
}
Solution* solutionCreate(int n, int* blacklist, int blacklistSize) {
Solution* obj = (Solution*)malloc(sizeof(Solution));
obj->map = (MapEntry**)calloc(HASH_SIZE, sizeof(MapEntry*));
int W = n - blacklistSize;
obj->W = W;
// Mark all blacklist elements for fast lookup
MapEntry** blacklistMap = (MapEntry**)calloc(HASH_SIZE, sizeof(MapEntry*));
for (int i = 0; i < blacklistSize; ++i) {
insert(blacklistMap, blacklist[i], -1);
}
int last = n - 1;
for (int i = 0; i < blacklistSize; ++i) {
if (blacklist[i] >= W) continue; // No need to remap
// Find a value in [W, n-1] not in blacklist
while (find(blacklistMap, last, NULL)) last--;
insert(obj->map, blacklist[i], last);
last--;
}
// Clean up
for (int i = 0; i < HASH_SIZE; ++i) {
MapEntry* node = blacklistMap[i];
while (node) {
MapEntry* tmp = node;
node = node->next;
free(tmp);
}
}
free(blacklistMap);
srand(time(NULL));
return obj;
}
int solutionPick(Solution* obj) {
int x = rand() % obj->W;
int mapped;
if (find(obj->map, x, &mapped)) {
return mapped;
}
return x;
}
void solutionFree(Solution* obj) {
for (int i = 0; i < HASH_SIZE; ++i) {
MapEntry* node = obj->map[i];
while (node) {
MapEntry* tmp = node;
node = node->next;
free(tmp);
}
}
free(obj->map);
free(obj);
}