deno.land / x / masx200_leetcode_test@10.6.5 / random-pick-index / 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
interface Solution { pick(target: number): number;}function Solution(nums: number[]): Solution { const pos: Map<number, number[]> = new Map(); for (const [i, num] of nums.entries()) { const indices: number[] = pos.get(num) ?? (() => { const d: number[] = []; pos.set(num, d); return d; })(); indices.push(i); } function pick(target: number): number { const indices = pos.get(target); if (!indices) { throw new Error("not found:" + target); } return indices[Math.floor(Math.random() * indices.length)]; } return { pick };}
export default Solution;
masx200_leetcode_test

Version Info

Tagged at
a year ago