Given an array of integers 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.
Note:
The array size can be very large. Solution that uses too much extra space will not pass the judge.
Example:
int[] nums = new int[] {1,2,3,3,3};
Solution solution = new Solution(nums);
// pick(3) should return either index 2, 3, or 4 randomly. Each index should have equal probability of returning.
solution.pick(3);
// pick(1) should return 0. Since in the array only nums[0] is equal to 1.
solution.pick(1);
方法1:
思路:
用到了reservoir sampling: https://en.wikipedia.org/wiki/Reservoir_sampling。
假设需要从n个数中随机抽取k个。当n太大无法全部储存的时候,使用streaming的方法,也就是保留一个大小为k的reservoir。当k不满的时候,留下新元素的可能性为1,当 n > k, e.g. k = 10, n = 11, replace某一个k元素的概率是k / n = 10 / 11,也就是它自己以1 / 11的概率被舍弃。而如果replace,以1 / 10 的概率随机选择[0, 9] 的一个数字,取代之。任一元素被舍弃的概率为10 / 11 * 1 / 10 = 1 / 11。新元素与旧元素被舍弃的概率相等 = 1 / n。
这道题里k = 1 也就是说,新来一个元素,我们要以1 / n 的概率更换掉当前reservoir里的元素,那么用rand()函数来实现就可以取rand() % n == 0 ,因为0的概率从n = 1开始,以1为起点递减。不用处理边界问题
易错点
- 更新count
class Solution {
public:
Solution(vector<int> nums) {
copy = nums;
}
int pick(int target) {
int count = 0;
int result = -1;
for (int i = 0; i < copy.size(); i++){
if (copy[i] == target){
count ++;
if (rand() % count == 0) result = i;
}
}
return result;
}
private:
vector<int> copy;
};
/**
* Your Solution object will be instantiated and called as such:
* Solution* obj = new Solution(nums);
* int param_1 = obj->pick(target);
*/