返回介绍

solution / 0700-0799 / 0710.Random Pick with Blacklist / README_EN

发布于 2024-06-17 01:03:35 字数 5286 浏览 0 评论 0 收藏 0

710. Random Pick with Blacklist

中文文档

Description

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.

Solutions

Solution 1

class Solution:
  def __init__(self, n: int, blacklist: List[int]):
    self.k = n - len(blacklist)
    self.d = {}
    i = self.k
    black = set(blacklist)
    for b in blacklist:
      if b < self.k:
        while i in black:
          i += 1
        self.d[b] = i
        i += 1

  def pick(self) -> int:
    x = randrange(self.k)
    return self.d.get(x, x)


# Your Solution object will be instantiated and called as such:
# obj = Solution(n, blacklist)
# param_1 = obj.pick()
class Solution {
  private Map<Integer, Integer> d = new HashMap<>();
  private Random rand = new Random();
  private int k;

  public Solution(int n, int[] blacklist) {
    k = n - blacklist.length;
    int i = k;
    Set<Integer> black = new HashSet<>();
    for (int b : blacklist) {
      black.add(b);
    }
    for (int b : blacklist) {
      if (b < k) {
        while (black.contains(i)) {
          ++i;
        }
        d.put(b, i++);
      }
    }
  }

  public int pick() {
    int x = rand.nextInt(k);
    return d.getOrDefault(x, x);
  }
}

/**
 * Your Solution object will be instantiated and called as such:
 * Solution obj = new Solution(n, blacklist);
 * int param_1 = obj.pick();
 */
class Solution {
public:
  unordered_map<int, int> d;
  int k;

  Solution(int n, vector<int>& blacklist) {
    k = n - blacklist.size();
    int i = k;
    unordered_set<int> black(blacklist.begin(), blacklist.end());
    for (int& b : blacklist) {
      if (b < k) {
        while (black.count(i)) ++i;
        d[b] = i++;
      }
    }
  }

  int pick() {
    int x = rand() % k;
    return d.count(x) ? d[x] : x;
  }
};

/**
 * Your Solution object will be instantiated and called as such:
 * Solution* obj = new Solution(n, blacklist);
 * int param_1 = obj->pick();
 */
type Solution struct {
  d map[int]int
  k int
}

func Constructor(n int, blacklist []int) Solution {
  k := n - len(blacklist)
  i := k
  black := map[int]bool{}
  for _, b := range blacklist {
    black[b] = true
  }
  d := map[int]int{}
  for _, b := range blacklist {
    if b < k {
      for black[i] {
        i++
      }
      d[b] = i
      i++
    }
  }
  return Solution{d, k}
}

func (this *Solution) Pick() int {
  x := rand.Intn(this.k)
  if v, ok := this.d[x]; ok {
    return v
  }
  return x
}

/**
 * Your Solution object will be instantiated and called as such:
 * obj := Constructor(n, blacklist);
 * param_1 := obj.Pick();
 */

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文