class Solution {
public:
Solution(int n, vector<int>& blacklist) {
}
int pick() {
}
};
/**
* Your Solution object will be instantiated and called as such:
* Solution* obj = new Solution(n, blacklist);
* int param_1 = obj->pick();
*/
class Solution {
private Map<Integer, Integer> map;
private int bound;
private Random random;
public Solution(int n, int[] blacklist) {
map = new HashMap<>();
bound = n - blacklist.length;
random = new Random();
Set<Integer> set = new HashSet<>();
for(int black: blacklist) {
set.add(black);
}
int i = bound;
for (int black: blacklist) {
if (black < bound) {
while(set.contains(i)) {
i++;
}
map.put(black, i++);
}
}
}
public int pick() {
int r = random.nextInt(bound);
return map.getOrDefault(r, r);
}
}
/**
* Your Solution object will be instantiated and called as such:
* Solution obj = new Solution(n, blacklist);
* int param_1 = obj.pick();
*/
class Solution:
def __init__(self, n: int, blacklist: List[int]):
m = len(blacklist)
self.bound = n - m
self.map = dict()
i = self.bound
bset = set(blacklist)
for black in blacklist:
if black < self.bound:
while i in bset:
i += 1
self.map[black] = i
i += 1
def pick(self) -> int:
return self.map.get(r:=randint(0, self.bound - 1), r)
# Your Solution object will be instantiated and called as such:
# obj = Solution(n, blacklist)
# param_1 = obj.pick()
/**
* @param {number} n
* @param {number[]} blacklist
*/
var Solution = function(n, blacklist) {
this.b2w = new Map();
const m = blacklist.length;
this.bound = n - m;
const black = new Set();
for (const b of blacklist) {
if (b >= this.bound) {
black.add(b);
}
}
let w = this.bound;
for (const b of blacklist) {
if (b < this.bound) {
while (black.has(w)) {
++w;
}
this.b2w.set(b, w);
++w;
}
}
};
/**
* @return {number}
*/
Solution.prototype.pick = function() {
const x = Math.floor(Math.random() * this.bound);
return this.b2w.get(x) || x;
};
/**
* Your Solution object will be instantiated and called as such:
* var obj = new Solution(n, blacklist)
* var param_1 = obj.pick()
*/
type Solution struct {
b2w map[int]int
bound int
}
func Constructor(n int, blacklist []int) Solution {
m := len(blacklist)
bound := n - m
black := map[int]bool{}
for _, b := range blacklist {
if b >= bound {
black[b] = true
}
}
b2w := make(map[int]int, m-len(black))
w := bound
for _, b := range blacklist {
if b < bound {
for black[w] {
w++
}
b2w[b] = w
w++
}
}
return Solution{b2w, bound}
}
func (s *Solution) Pick() int {
x := rand.Intn(s.bound)
if s.b2w[x] > 0 {
return s.b2w[x]
}
return x
}
/**
* Your Solution object will be instantiated and called as such:
* obj := Constructor(n, blacklist);
* param_1 := obj.Pick();
*/
class Solution:
def __init__(self, n: int, blacklist: List[int]):
m = len(blacklist)
self.bound = w = n - m
black = {b for b in blacklist if b >= self.bound}
self.b2w = {}
for b in blacklist:
if b < self.bound:
while w in black:
w += 1
self.b2w[b] = w
w += 1
def pick(self) -> int:
x = randrange(self.bound)
return self.b2w.get(x, x)
# Your Solution object will be instantiated and called as such:
# obj = Solution(n, blacklist)
# param_1 = obj.pick()