LeetCode //C - 710. Random Pick with Blacklist

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,n1)
  • 0 <= blacklist[i] < n
  • All the values of blacklist are unique.
  • At most 2 ∗ 1 0 4 2 * 10^4 2104 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);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Navigator_Z

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值