382. Linked List Random Node

2016-09-07  本文已影响52人  codingXue

问题描述

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

问题分析

在不知道链表长度n的情况下,不能直接利用1/n的概率信息。
做法是,首先选中第1个数字,在读到第k个数字的时候,以1/k的概率用第k个数字替换当前数字,以(1-1/k)的概率忽略第k个数字。
证明如下:

扩展问题:
Reservoir Sampling: Maintain a sample of size 𝑠 drawn (without replacement) from all elements in the stream so far.

做法:

证明:
使用类似上面的方式进行证明,仅可以证明所有数字进入最终集合的概率一样,并不能证明任何一个k元素子集被选中的概率相同。
完整的证明还没看懂...

AC代码

# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution(object):

    def __init__(self, head):
        """
        @param head The linked list's head.
        Note that the head is guaranteed to be not null, so it contains at least one node.
        :type head: ListNode
        """
        self.head = head

    def getRandom(self):
        """
        Returns a random node's value.
        :rtype: int
        """
        r = self.head
        p = r.next
        n = 1
        while p:
            n += 1
            x = random.randint(1, n)
            if x == 1:
                r = p
            p = p.next
        return r.val

# Your Solution object will be instantiated and called as such:
# obj = Solution(head)
# param_1 = obj.getRandom()
上一篇 下一篇

猜你喜欢

热点阅读