> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Random Pick with Blacklist Python Solution

> Tested Python solution for LeetCode 710 with 16 pytest cases. Generate a practice environment with lcpy.

LeetCode 710, [Hard](/catalog/hard). Topics: [Array](/catalog/topics/array), [Hash Table](/catalog/topics/hash-table), [Math](/catalog/topics/math), [Binary Search](/catalog/topics/binary-search), [Sorting](/catalog/topics/sorting), Randomized. [View on LeetCode](https://leetcode.com/problems/random-pick-with-blacklist/description/).

Generate this problem as a practice environment: tested reference solution, 16 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 710   # by problem number
lcpy gen -s random_pick_with_blacklist   # by problem name
```

## Problem

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`.

### Examples

```
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 <= 10^9`
* `0 <= blacklist.length <= min(10^5, n - 1)`
* `0 <= blacklist[i] < n`
* All the values of `blacklist` are **unique**.
* At most `2 * 10^4` calls will be made to `pick`.

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/random_pick_with_blacklist/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/random_pick_with_blacklist/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
import random


class Solution:
    # Time: O(b) init, O(1) pick
    # Space: O(b)
    def __init__(self, n: int, blacklist: list[int]) -> None:
        self.size = n - len(blacklist)
        black = set(blacklist)
        tail = [x for x in range(self.size, n) if x not in black]
        self.remap: dict[int, int] = {}
        for i, b in enumerate(sorted(b for b in black if b < self.size)):
            self.remap[b] = tail[i]

    def pick(self) -> int:
        idx = random.randint(0, self.size - 1)
        return self.remap.get(idx, idx)
```

## Complexity

| Time | Space |
| - | - |
| O(b) init, O(1) pick | O(b) |

## Tags


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.