Design a class Solution over an integer array that may contain duplicates.
new Solution(nums, rng) stores the array and the generator. rng returns numbers in [0, 1)pick(target) returns an index i with nums[i] === target, chosen uniformly among all such indices- If the target is not in the array, return
-1 - Use reservoir sampling over the matching indices, with O(1) extra memory: do not build a map of index lists
- For the m-th match you see (counting from 1), draw once with
Math.floor(rng() * m) and take this index if the result is 0. That is one draw per match, including the first, and no draws for other elements