问题描述
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个数字。
证明如下:
- 当n=1时,显然正确;
- 假设当从前k个元素进行选取时,k个数字被选取的概率均为1/k;
- 当加入第k+1个元素,第k+1个元素被选取的概率是1/(k+1),而前k个元素被选取的概率为1/k x k/(k+1) = 1/(k+1);
扩展问题:
Reservoir Sampling: Maintain a sample of size 𝑠 drawn (without replacement) from all elements in the stream so far.
做法:
- Keep the first 𝑠 elements in the stream, set 𝑛←𝑠
- Algorithm for a new element
𝑛←𝑛+1;
With probability 𝑠/𝑛, use it to replace an item in the current sample chosen uniformly at random;
With probability 1−𝑠/𝑛, throw it away;
证明:
使用类似上面的方式进行证明,仅可以证明所有数字进入最终集合的概率一样,并不能证明任何一个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()