
nums를 순회하며, 요소를 key로 갖는 각 인덱스 저장pick 호출 시, target에 해당하는 인덱스를 중 랜덤한 인덱스를 반환class Solution {
private map: Map<number, number[]>
constructor(nums: number[]) {
const map = new Map()
for(let i = 0; i < nums.length; i++) {
map.set(nums[i], [...(map.get(nums[i]) ?? []), i])
}
this.map = map
}
pick(target: number): number {
const indexies = this.map.get(target)
const rand = Math.floor(Math.random() * indexies.length)
return indexies[rand]
}
}