710. Random Pick with Blacklist LeetCode Solution

In this guide, you will get 710. Random Pick with Blacklist LeetCode Solution with the best time and space complexity. The solution to Random Pick with Blacklist problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Random Pick with Blacklist solution in C++
  4. Random Pick with Blacklist solution in Java
  5. Random Pick with Blacklist solution in Python
  6. Additional Resources
710. Random Pick with Blacklist LeetCode Solution image

Problem Statement of 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

See also  1035. Uncrossed Lines LeetCode Solution

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.

Complexity Analysis

  • Time Complexity: O(|\texttt{blacklist}|)
  • Space Complexity: O(|\texttt{blacklist}|)

710. Random Pick with Blacklist LeetCode Solution in C++

class Solution {
 public:
  Solution(int n, vector<int>& blacklist) : validRange(n - blacklist.size()) {
    for (const int b : blacklist)
      map[b] = -1;

    int maxAvailable = n - 1;

    for (const int b : blacklist)
      if (b < validRange) {
        // Find the slot that haven't been used.
        while (map.contains(maxAvailable))
          --maxAvailable;
        map[b] = maxAvailable--;
      }
  }

  int pick() {
    const int num = rand() % validRange;
    const auto it = map.find(num);
    return it == map.cend() ? num : it->second;
  }

 private:
  const int validRange;
  unordered_map<int, int> map;
};
/* code provided by PROGIEZ */

710. Random Pick with Blacklist LeetCode Solution in Java

class Solution {
  public Solution(int n, int[] blacklist) {
    validRange = n - blacklist.length;

    for (final int b : blacklist)
      map.put(b, -1);

    int maxAvailable = n - 1;

    for (final int b : blacklist)
      if (b < validRange) {
        // Find the slot that haven't been used.
        while (map.containsKey(maxAvailable))
          --maxAvailable;
        map.put(b, maxAvailable--);
      }
  }

  public int pick() {
    final int num = rand.nextInt(validRange);
    return map.getOrDefault(num, num);
  }

  private int validRange;
  private Map<Integer, Integer> map = new HashMap<>();
  private Random rand = new Random();
}
// code provided by PROGIEZ

710. Random Pick with Blacklist LeetCode Solution in Python

class Solution:
  def __init__(self, n: int, blacklist: list[int]):
    self.validRange = n - len(blacklist)
    self.dict = {}

    maxAvailable = n - 1

    for b in blacklist:
      self.dict[b] = -1

    for b in blacklist:
      if b < self.validRange:
        # Find the slot that haven't been used.
        while maxAvailable in self.dict:
          maxAvailable -= 1
        self.dict[b] = maxAvailable
        maxAvailable -= 1

  def pick(self) -> int:
    value = random.randint(0, self.validRange - 1)

    if value in self.dict:
      return self.dict[value]

    return value
# code by PROGIEZ

Additional Resources

See also  786. K-th Smallest Prime Fraction LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.