deno.land / x / masx200_leetcode_test@10.6.5 / random-pick-with-blacklist / index.ts

نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
export default class Solution { #array: Array<number>; #black: Set<number>; #n: number; constructor(n: number, blacklist: number[]) { this.#n = n; const black = new Set(blacklist); this.#black = black; this.#array = new Array<number>(); if ( n - blacklist.length <= 50000 && (n - blacklist.length) / n <= 0.5 ) { for (let i = 0; i < n; i++) { if (!black.has(i)) { this.#array.push(i); } } } }
pick(): number { if (this.#array.length) { const index = Math.floor(Math.random() * this.#array.length); return this.#array[index]; } else { while (true) { const index = Math.floor(Math.random() * this.#n);
if (!this.#black.has(index)) { return index; } } } }}
masx200_leetcode_test

Version Info

Tagged at
a year ago