Problem
Given an integer array nums
with possible duplicates, randomly output the index of a given target
number. You can assume that the given target number must exist in the array.
Implement the Solution
class:
Solution(int[] nums)
Initializes the object with the arraynums
.int pick(int target)
Picks a random indexi
fromnums
wherenums[i] == target
. If there are multiple valid i’s, then each index should have an equal probability of returning.
Examples
Example 1:
|
|
Solution
Method 1 - Linear Search and Reservoir Sampling
We previously discussed this here: Reservoir Sampling Explained. Now, using the example: {1, 2, 3, 3, 3} with target 3, we aim to select indices 2, 3, and 4, each with a probability of 1/3.
- Index 2: The probability of selection is 1 * (1/2) * (2/3) = 1/3.
- Index 3: The probability of selection is (1/2) * (2/3) = 1/3.
- Index 4: The probability of selection is 1/3.
Code
|
|
Complexity
Constructor: Solution(int[] nums)
- ⏰ Time complexity:
O(1)
- 🧺 Space complexity:
O(1)
Method: int pick(int target)
- ⏰ Time complexity:
O(n)
- 🧺 Space complexity:
O(1)
Method 2 - Using Map of Number and List of Indices 🏆
We can also do following:
- Create a map where the
key
is an integer (anumber
from the input array) and thevalue
is alist of integers
(indices where this number appears in the array). - Generate a random integer
i
between0
(inclusive) and the size ofcurrIndices
(exclusive). - Return the element at the randomly selected index from
currIndices
.
Code
|
|
Complexity
Constructor: Solution(int[] nums)
- ⏰ Time complexity:
O(n)
- 🧺 Space complexity:
O(n)
Method: int pick(int target)
- ⏰ Time complexity:
O(1)
- 🧺 Space complexity:
O(1)