We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent be15c9d commit f148338Copy full SHA for f148338
710-random-pick-with-blacklist.js
@@ -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