<p>Given an integer array <code>nums</code> with possible <strong>duplicates</strong>, randomly output the index of a given <code>target</code> number. You can assume that the given target number must exist in the array.</p>

<p>Implement the <code>Solution</code> class:</p>

<ul>
	<li><code>Solution(int[] nums)</code> Initializes the object with the array <code>nums</code>.</li>
	<li><code>int pick(int target)</code> Picks a random index <code>i</code> from <code>nums</code> where <code>nums[i] == target</code>. If there are multiple valid i&#39;s, then each index should have an equal probability of returning.</li>
</ul>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input</strong>
[&quot;Solution&quot;, &quot;pick&quot;, &quot;pick&quot;, &quot;pick&quot;]
[[[1, 2, 3, 3, 3]], [3], [1], [3]]
<strong>Output</strong>
[null, 4, 0, 2]

<strong>Explanation</strong>
Solution solution = new Solution([1, 2, 3, 3, 3]);
solution.pick(3); // It should return either index 2, 3, or 4 randomly. Each index should have equal probability of returning.
solution.pick(1); // It should return 0. Since in the array only nums[0] is equal to 1.
solution.pick(3); // It should return either index 2, 3, or 4 randomly. Each index should have equal probability of returning.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= nums.length &lt;= 2 * 10<sup>4</sup></code></li>
	<li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li>
	<li><code>target</code> is an integer from <code>nums</code>.</li>
	<li>At most <code>10<sup>4</sup></code> calls will be made to <code>pick</code>.</li>
</ul>

from collections import defaultdict
import random
class Solution1:

    def __init__(self, nums: List[int]):
        
        self.nums = nums
        self.lookup = defaultdict(list)        

    def pick(self, target: int) -> int:
        
        for i, num in enumerate(self.nums):
            
            self.lookup[num].append(i)
            
        if len(self.lookup[target]) == 1:
            return self.lookup[target][0]
        else:
            rand_idx = random.randint(0, len(self.lookup[target])-1)
            return self.lookup[target][rand_idx]
            
class Solution2:

    def __init__(self, nums: List[int]):
        
        self.nums = nums
                

    def pick(self, target: int) -> int:
        
        count = 0
        ans = -1
        
        for i, num in enumerate(self.nums):
            if num == target:
                count += 1
                if count == 1 or (count > 1 and randint(0, count - 1) == 0):
                    ans = i
                
        return ans          
class Solution3:      
    def __init__(self, nums: List[int]):
        self.nums = nums

    def pick(self, target: int) -> int:
        cnt = idx = 0
        for i, num in enumerate(self.nums):
            if num != target:
                continue
            if cnt == 0:
                idx = i
                cnt = 1
            else:
                # this random will already give me numbers
                # between 0 and cnt inclusive
                # so for 2nd number I am getting random number 0 and 1
                # so each having a probability of 1/2
                # similarly for three numbers it will be 1/3
                rnd = random.randint(0, cnt)
                if (rnd == cnt):
                    idx = i
                cnt += 1
        
        return idx          


from random import randint

class Solution:
    def __init__(self, nums: List[int]):
        # save nums array for reservoir sampling for the first
        # call to pick
        self.nums = nums
        # save lists of indices to skip reservoir sampling
        # after we've done one pick
        self.indices = defaultdict(list)

    def pick(self, target: int) -> int:
        # if we've already called pick, just randomly select
        # from the saved index list
        if target in self.indices:
            return self.indices[target][
                randint(0, len(self.indices[target])-1)
            ]
        
        # otherwise, iterate through the whole list for reservoir
        # sampling
        r = None
        count = 0

        for i, n in enumerate(self.nums):
            if n == target:
                if r is None or randint(0, count) == 0:
                    r = i
                count += 1

            # build up lists of indices
            self.indices[n].append(i)
        return r                
                
        


# Your Solution object will be instantiated and called as such:
# obj = Solution(nums)
# param_1 = obj.pick(target)
24100