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. ProFTPD <=1.3.5 mod_copy 未授权文件复制漏洞

    poc如下: #!/usr/bin/env python# coding=utf-8 """Site: http://www.beebeeto.com/Framework ...

  2. oracle 11g express 修改oem端口

    begin dbms_xdb.sethttpport('8081'); dbms_xdb.setftpport('0'); end; / 这样就把端口设置为8081了.

  3. jQuery简单实现iframe的高度根据页面内容自适应的方法

    同域下: //注意:下面的代码是放在和iframe同一个页面中调用 $("#myiframe").load(function () { var myiframeH = $(this ...

  4. App Extension Today

     App Extensions 是iOS8新开放的扩展机制,之后不断增加功能.App Extension Programming Guide: Today   不喜欢废话,直接上干货!   一:重要概 ...

  5. php empty函数

    empty — 检查一个变量是否为空. 当一个变量并不存在,或者它的值等同于FALSE,那么它就会被认为不存在.如果变量不存在的话,empty()并不会产生警告. 返回值: 当var存在,并且是一个非 ...

  6. 1.Redis安装(转)

    Redis的官网为: http://redis.io/. 1.Redis安装 redis的安装非常的简单,而且Redis并不依赖其他环境和标准库,很容易上手,这可能也是它流行的一个原因.这里为了测试方 ...

  7. Fortify

    sourceanalyzer -b my_buildid -scan -f xxx.fpr -b  取一个build的ID号,通常以这个项目名称加扫描时间为buildID-Xmx 指定JVM使用的最大 ...

  8. 关于php cgi的配置

    http://blog.csdn.net/xiaolei1982/article/details/7103850 1,查看php-cgi的进程数 netstat -anpo | grep " ...

  9. R语言学习笔记

    向量化的函数 向量化的函数 ifelse/which/where/any/all/cumsum/cumprod/对于矩阵而言,可以使用rowSums/colSums.对于“穷举所有组合问题" ...

  10. H5移动端知识点总结

    H5移动端知识点总结 阅读目录 移动开发基本知识点 calc基本用法 box-sizing的理解及使用 理解display:box的布局 理解flex布局 Flex布局兼容知识点总结 回到顶部 移动开 ...