Skip to content

Commit f148338

Browse files
authored
Create 710-random-pick-with-blacklist.js
1 parent be15c9d commit f148338

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

710-random-pick-with-blacklist.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* @param {number} N
3+
* @param {number[]} blacklist
4+
*/
5+
const Solution = function (N, blacklist) {
6+
this.map = new Map()
7+
for (let b of blacklist) this.map.set(b, -1)
8+
this.M = N - this.map.size
9+
for (let b of blacklist) {
10+
if (b < this.M) {
11+
while (this.map.has(N - 1)) N--
12+
this.map.set(b, N - 1)
13+
N--
14+
}
15+
}
16+
}
17+
18+
/**
19+
* @return {number}
20+
*/
21+
Solution.prototype.pick = function () {
22+
const p = Math.floor(Math.random() * this.M)
23+
if (this.map.has(p)) return this.map.get(p)
24+
return p
25+
}
26+
27+
/**
28+
* Your Solution object will be instantiated and called as such:
29+
* var obj = new Solution(N, blacklist)
30+
* var param_1 = obj.pick()
31+
*/

0 commit comments

Comments
 (0)