问题:如何随机从n个对象中选择一个对象,这n个对象是按序排列的,但是在此之前你是不知道n的值的。
思路:如果我们能够知道n的值,那么问题就简单了,只需要randInt(n)得到确切的位置,然后选择出该位置的值即可,实现概率为1/n的抽样。
但现在我们并不知道n的值,这个问题便抽象为蓄水池抽样,即从一个包含n个对象的列表S中随机选取k个对象,n为一个非常大或者不知道的值。通常情况下,n是一个非常大的值,大到无法一次性把所有列表S中的对象都放到内存中。我们这个问题是蓄水池抽样问题的一个特例,即k=1。
解法:我们总是选择第一个对象,以1/2的概率选择第二个,以1/3的概率选择第三个,以此类推,以1/m的概率选择第m个对象。当该过程结束时,每一个对象具有相同的选中概率,即1/n,证明如下。
证明:第m个对象最终被选中的概率P=选择m的概率*其后面所有对象不被选择的概率,即
P = 1/m * (m/(m+1) * (m+1)/(m+2) * (m+2)/(m+3) * ...* (n-1)/n)
= 1/n
对应蓄水池抽样问题,可以类似的思路解决。先把读到的前k个对象放入“水库”,对于第k+1个对象开始,以k/(k+1)的概率选择该对象,以k/(k+2)的概率选择第k+2个对象,以此类推,以k/m的概率选择第m个对象(m>k)。如果m被选中,则随机替换水库中的一个对象。最终每个对象被选中的概率均为k/n,证明如下。
证明:第m个对象被选中的概率=选择m的概率*(其后元素不被选择的概率+其后元素被选择的概率*不替换第m个对象的概率),即
P = k/m * (((m+1-k)/(m+1) + k/(m+1) * (k-1)/k ) * ((m+2-k)/(m+2) + k/(m+2) * (k-1)/k ) * ... * ((n-k)/n + k/n*(k-1)/k))
= k/n
参考:http://blog.csdn.net/huagong_adu/article/details/7619665
题目描述:
Given a singly linked list, return a random node's value from the linked list. Each node must have the same probability of being chosen.
Follow up:
What if the linked list is extremely large and its length is unknown to you? Could you solve this efficiently without using extra space?
Example:
// Init a singly linked list [1,2,3]. ListNode head = new ListNode(1); head.next = new ListNode(2); head.next.next = new ListNode(3); Solution solution = new Solution(head); // getRandom() should return either 1, 2, or 3 randomly. Each element should have equal probability of returning. solution.getRandom();
解题思路:
蓄水池抽样
代码描述:
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */ public class Solution { ListNode head = null; Random random = null; /** @param head The linked list's head. Note that the head is guaranteed to be not null, so it contains at least one node. */ public Solution(ListNode head) { this.head = head; this.random = new Random(); } /** Returns a random node's value. */ public int getRandom() { ListNode current = head; int res = 0; for(int i = 1; current != null; i++){ if(random.nextInt(i) == 0) res = current.val; current = current.next; } return res; } } /** * Your Solution object will be instantiated and called as such: * Solution obj = new Solution(head); * int param_1 = obj.getRandom(); */