美文网首页
382. Linked List Random Node 链表的

382. Linked List Random Node 链表的

作者: 这就是一个随意的名字 | 来源:发表于2017-07-30 10:43 被阅读0次

    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();
    

    给定一单链表,返回一随机节点的值,每个节点被选中的概率必须一致。
    进阶:
    如果单链表非常大,且其长度未知的情况将如何?能否在不使用额外空间的情况下解决这个问题?


    思路
    【Reservoir Sampling 蓄水池抽样问题】
    (可理解为为等概抽样问题)

    • 问题:n个数中抽取k个,确保每个数被抽中的概率为n/k。

    • 基本思路:

      1. 先选取1,2,3,...,k将之放入蓄水池;
      2. 对于k+1,将之以k/(k+1)的概率抽取,然后随机替换水池中的一个数。
      3. 对于k+i,将之以k/(k+i)的概率抽取,然后随机替换水池中的一个数。
      4. 重复上述,直到k+i到达n;
    • 证明:
      对于k+i,其选中并替换水池中已有元素的概率为k/(k+i)
      对于水池中的某数x,其之前就在水池,一次替换后仍在水池中的概率是
      P(x之前在水池) * P(未被k+i替换)
      =P(x之前在水池) * (1-P(k+i被选中且替换了x) )
      = k/(k+i-1) × (1 - k/(k+i) × 1/k)
      = k/(k+i)
      当k+i到达n,则结果为k/n

    • 举例

      • Choose 3 numbers from [111, 222, 333, 444]. Make sure each number is selected with a probability of 3/4
      • First, choose [111, 222, 333] as the initial reservior
      • Then choose 444 with a probability of 3/4
      • For 111, it stays with a probability of
        • P(444 is not selected) + P(444 is selected but it replaces 222 or 333)= 1/4 + 3/4*2/3= 3/4
      • The same case with 222 and 333
      • Now all the numbers have the probability of 3/4 to be picked

    对于本题,取k=1即可

    /**
     * Definition for singly-linked list.
     * struct ListNode {
     *     int val;
     *     ListNode *next;
     *     ListNode(int x) : val(x), next(NULL) {}
     * };
     */
    
    class Solution {
    private:
        ListNode* head;
    public:
        /** @param head The linked list's head. Note that the head is guanranteed to be not null, so it contains at least one node. */
        Solution(ListNode* head) {
            this->head = head;
        }
    
        /** Returns a random node's value. */
        int getRandom() {
            int res = head->val;
            ListNode* node = head->next;
            int i = 2;
            while(node){
                int j = rand()%i;
                if(j==0)
                    res = node->val;
                i++;
                node = node->next;
            }
            return res;
        }
    };
    
    /**
     * Your Solution object will be instantiated and called as such:
     * Solution obj = new Solution(head);
     * int param_1 = obj.getRandom();
     */
    

    相关文章

      网友评论

          本文标题:382. Linked List Random Node 链表的

          本文链接:https://www.haomeiwen.com/subject/ffmhlxtx.html