|
| 1 | +/** |
| 2 | + * [0710] Random Pick with Blacklist |
| 3 | + * |
| 4 | + * 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. |
| 5 | + * Optimize your algorithm such that it minimizes the number of calls to the built-in random function of your language. |
| 6 | + * Implement the Solution class: |
| 7 | + * |
| 8 | + * Solution(int n, int[] blacklist) Initializes the object with the integer n and the blacklisted integers blacklist. |
| 9 | + * int pick() Returns a random integer in the range [0, n - 1] and not in blacklist. |
| 10 | + * |
| 11 | + * |
| 12 | + * Example 1: |
| 13 | + * |
| 14 | + * Input |
| 15 | + * ["Solution", "pick", "pick", "pick", "pick", "pick", "pick", "pick"] |
| 16 | + * [[7, [2, 3, 5]], [], [], [], [], [], [], []] |
| 17 | + * Output |
| 18 | + * [null, 0, 4, 1, 6, 1, 0, 4] |
| 19 | + * Explanation |
| 20 | + * Solution solution = new Solution(7, [2, 3, 5]); |
| 21 | + * solution.pick(); // return 0, any integer from [0,1,4,6] should be ok. Note that for every call of pick, |
| 22 | + * // 0, 1, 4, and 6 must be equally likely to be returned (i.e., with probability 1/4). |
| 23 | + * solution.pick(); // return 4 |
| 24 | + * solution.pick(); // return 1 |
| 25 | + * solution.pick(); // return 6 |
| 26 | + * solution.pick(); // return 1 |
| 27 | + * solution.pick(); // return 0 |
| 28 | + * solution.pick(); // return 4 |
| 29 | + * |
| 30 | + * |
| 31 | + * Constraints: |
| 32 | + * |
| 33 | + * 1 <= n <= 10^9 |
| 34 | + * 0 <= blacklist.length <- min(10^5, n - 1) |
| 35 | + * 0 <= blacklist[i] < n |
| 36 | + * All the values of blacklist are unique. |
| 37 | + * At most 2 * 10^4 calls will be made to pick. |
| 38 | + * |
| 39 | + */ |
| 40 | +// problem: https://leetcode.com/problems/random-pick-with-blacklist/ |
| 41 | +// discuss: https://leetcode.com/problems/random-pick-with-blacklist/discuss/?currentPage=1&orderBy=most_votes&query= |
| 42 | + |
| 43 | +// submission codes start here |
| 44 | +use rand::prelude::*; |
| 45 | +use std::collections::HashMap; |
| 46 | + |
| 47 | +struct Solution { |
| 48 | + rng: ThreadRng, |
| 49 | + map: HashMap<i32, i32>, |
| 50 | + m: i32, |
| 51 | +} |
| 52 | + |
| 53 | +/** |
| 54 | + * `&self` means the method takes an immutable reference. |
| 55 | + * If you need a mutable reference, change it to `&mut self` instead. |
| 56 | + */ |
| 57 | +impl Solution { |
| 58 | + fn new(n: i32, blacklist: Vec<i32>) -> Self { |
| 59 | + let rng = Default::default(); |
| 60 | + |
| 61 | + let mut map = std::collections::HashMap::<i32, i32>::new(); |
| 62 | + for &b in &blacklist { |
| 63 | + map.insert(b, -1); |
| 64 | + } |
| 65 | + |
| 66 | + let mut n = n; |
| 67 | + let m = n - map.len() as i32; |
| 68 | + for b in blacklist { |
| 69 | + if b < m { |
| 70 | + while map.contains_key(&(n - 1)) { |
| 71 | + n -= 1; |
| 72 | + } |
| 73 | + map.insert(b, n - 1); |
| 74 | + n -= 1; |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + Self { rng, map, m } |
| 79 | + } |
| 80 | + |
| 81 | + fn pick(&mut self) -> i32 { |
| 82 | + let p = self.rng.gen_range(0, self.m); |
| 83 | + if self.map.contains_key(&p) { |
| 84 | + *self.map.get(&p).unwrap() |
| 85 | + } else { |
| 86 | + p |
| 87 | + } |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +// submission codes end |
| 92 | + |
| 93 | +#[cfg(test)] |
| 94 | +mod tests { |
| 95 | + use super::*; |
| 96 | + |
| 97 | + #[test] |
| 98 | + fn test_0710_example_1() { |
| 99 | + let mut solution = Solution::new(7, vec![2, 3, 5]); |
| 100 | + solution.pick(); // return 0, any integer from [0,1,4,6] should be ok. Note that for every call of pick, |
| 101 | + // 0, 1, 4, and 6 must be equally likely to be returned (i.e., with probability 1/4). |
| 102 | + solution.pick(); // return 4 |
| 103 | + solution.pick(); // return 1 |
| 104 | + solution.pick(); // return 6 |
| 105 | + solution.pick(); // return 1 |
| 106 | + solution.pick(); // return 0 |
| 107 | + solution.pick(); // return 4 |
| 108 | + } |
| 109 | +} |
0 commit comments