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);
 public class Solution {

     int[] nums;
Random r = new Random(); public Solution(int[] nums) {
this.nums = nums;
} public int pick(int target) {
ArrayList<Integer> idxs = new ArrayList<Integer>();
for (int i = ; i < nums.length; i++) {
if (target == nums[i]) {
idxs.add(i);
}
}
return idxs.get(r.nextInt(idxs.size()));
}
}

Simple Reservior Sampling approach

 public class Solution {

     int[] nums;
Random rnd; public Solution(int[] nums) {
this.nums = nums;
this.rnd = new Random();
} public int pick(int target) {
int result = -;
int count = ;
for (int i = ; i < nums.length; i++) {
if (nums[i] != target)
continue;
if (rnd.nextInt(++count) == )
result = i;
} return result;
}
}

Simple Reservior Sampling

Suppose we see a sequence of items, one at a time. We want to keep a single item in memory, and we want it to be selected at random from the sequence. If we know the total number of items (n), then the solution is easy: select an index ibetween 1 and n with equal probability, and keep the i-th element. The problem is that we do not always know n in advance. A possible solution is the following:

  • Keep the first item in memory.
  • When the i-th item arrives (for {\displaystyle i>1}):
    • with probability {\displaystyle 1/i}, keep the new item (discard the old one)
    • with probability {\displaystyle 1-1/i}, keep the old item (ignore the new one)

So:

  • when there is only one item, it is kept with probability 1;
  • when there are 2 items, each of them is kept with probability 1/2;
  • when there are 3 items, the third item is kept with probability 1/3, and each of the previous 2 items is also kept with probability (1/2)(1-1/3) = (1/2)(2/3) = 1/3;
  • by induction, it is easy to prove that when there are n items, each item is kept with probability 1/n.

Random Pick Index的更多相关文章

  1. 398. Random Pick Index - LeetCode

    Question 398. Random Pick Index Solution 思路:重点是如果数据中有多个数target相等,要从这些数中随机取一个,根据例题 假设输入是: int[] nums ...

  2. [LeetCode] Random Pick Index 随机拾取序列

    Given an array of integers with possible duplicates, randomly output the index of a given target num ...

  3. Leetcode: Random Pick Index

    Given an array of integers with possible duplicates, randomly output the index of a given target num ...

  4. [Swift]LeetCode398. 随机数索引 | Random Pick Index

    Given an array of integers with possible duplicates, randomly output the index of a given target num ...

  5. [LeetCode] 398. Random Pick Index ☆☆☆

    Given an array of integers with possible duplicates, randomly output the index of a given target num ...

  6. 398. Random Pick Index随机pick函数

    [抄题]: Given an array of integers with possible duplicates, randomly output the index of a given targ ...

  7. [LC] 398. Random Pick Index

    Given an array of integers with possible duplicates, randomly output the index of a given target num ...

  8. 【LeetCode】398. Random Pick Index 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 每次遍历索引 字典保存索引 蓄水池抽样 日期 题目地 ...

  9. [leetcode] 398. Random Pick Index

    我是链接 看到这道题,想到做的几道什么洗牌的题,感觉自己不是很熟,但也就是rand()函数的调用,刚开始用map<int, vector<int >>来做,tle,后来就想着直 ...

随机推荐

  1. python 日志收集系统

    服务器端: #!/usr/bin/env python # -*- coding:utf-8 -*- import socket ip_port = ('0.0.0.0',9999) sk = soc ...

  2. C语言Hello world

    #include"stdio.h" void main() { printf("Hello world!\n"); }

  3. intelligencia.urlrewriter使用

    见github: https://github.com/sethyates/urlrewriter

  4. hibernate4中使用Session doWork()方法进行jdbc操作(代码)

    Hibernate3.3.2版本中getSession().connection()已被弃用,hibernate4中官方推荐使用Session doWork()方法进行jdbc操作 首先看看Work接 ...

  5. CF467C George and Job (DP)

    Codeforces Round #267 (Div. 2) C. George and Job time limit per test 1 second memory limit per test ...

  6. javascript客户端检测技术

    1. Firefox  Gecko是firefox的呈现引擎.当初的Gecko是作为通用Mozilla浏览器一部分开发的,而第一个采用Gecko引擎的浏览器是Netscape6: 我们可以使用用户代理 ...

  7. 黄学长模拟day1 大逃亡

    给出数字N(1<=N<=10000),X(1<=x<=1000),Y(1<=Y<=1000),代表有N个敌人分布一个X行Y列的矩阵上,矩形的行号从0到X-1,列号从 ...

  8. java框架

    Dash Reports 1.0发布 Java报表解决方案 http://developer.51cto.com/art/201205/337189.htm http://www.oschina.ne ...

  9. Oracle的分区操作和修改分区主键

    1.增加一个分区ALTER TABLE sales ADD PARTITION jan96 VALUES LESS THAN ( '01-FEB-1999' ) TABLESPACE tsx;增加一个 ...

  10. UESTC 1852 Traveling Cellsperson

    找规律水题... Traveling Cellsperson Time Limit: 1000ms Memory Limit: 65535KB This problem will be judged ...