Skip to main content
Back to problems
#710
Hard Algorithms

Random pick with blacklist

Array Hash Table Math Binary Search Sorting Randomized
34.6% acceptance
Feb 21, 2026
896
121
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.

Solution

Rust
Time O(n²)
Space O(n)
LeetCode
solution.rs
/*
 * 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.

 * struct Solution {

 * }


 * /** 
 *  * `&self` means the method takes an immutable reference.
 *  * If you need a mutable reference, change it to `&mut self` instead.
 *  */
 * impl Solution {

 *     fn new(n: i32, blacklist: Vec<i32>) -> Self {

 *     }

 *     fn pick(&self) -> i32 {

 *     }
 * }
 */
use std::collections::HashMap;

struct Solution {
  m: i32,
  remap: HashMap<i32, i32>,
}

impl Solution {
  fn new(n: i32, blacklist: Vec<i32>) -> Self {
    let b = blacklist.len() as i32;
    let m = n - b;
    let black_set: std::collections::HashSet<i32> = blacklist.iter().cloned().collect();
    let mut remap = HashMap::new();
    let mut white = m;
    for &bl in &blacklist {
      if bl < m {
        while black_set.contains(&white) { white += 1; }
        remap.insert(bl, white);
        white += 1;
      }
    }
    Solution { m, remap }
  }

  fn pick(&self) -> i32 {
    let r = (rand_usize() % self.m as usize) as i32;
    *self.remap.get(&r).unwrap_or(&r)
  }
}

fn rand_usize() -> usize {
  
  static mut SEED: u64 = 12345;
  unsafe {
    SEED ^= SEED << 13;
    SEED ^= SEED >> 7;
    SEED ^= SEED << 17;
    SEED as usize
  }
}