[leetcode] 382. Linked List Random Node

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

这道题是在未知长度链表中等概率的随机取节点,题目难度为Medium。

由于每次取随机节点时不知道链表长度,所以不能简单用rand()函数来取随机节点。这里介绍下蓄水池抽样的基本思路,了解的同学可以跳过。假定最终选出的节点为node,第一次直接选第一个节点;第二次以1/2的概率决定是否拿第二个节点替换node;第三次以1/3的概率决定是否拿第三个节点替换node;以此类推,直至链表最后一个节点。这种方法能够保证所有节点被选中的概率均为1/n(n为当前节点数),即保证了等概率的随机选取。证明如下:

对于第m个节点,被选中的概率为:它被选中的概率 * 之后节点都没被选中的概率,即(1/m) * (m/(m+1)) * ((m+1)/(m+2)) * ... * ((n-1)/n) = 1/n.

具体代码:

class Solution {
    ListNode* hd;
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) : hd(head) {}
    
    /** Returns a random node's value. */
    int getRandom() {
        ListNode* node = hd;
        int cnt = 0, randVal = 0;
        while(node) {
            if(rand() % (++cnt) == 0)
                randVal = node->val;
            node = node->next;
        }
        return randVal;
    }
};

/**
 * Your Solution object will be instantiated and called as such:
 * Solution obj = new Solution(head);
 * int param_1 = obj.getRandom();
 */

还可以将问题扩展到在未知或大样本中等概率随机取k个节点,思路是一样的。首先选出前k个节点放入蓄水池;第k+1个节点以k/(k+1)的概率决定是否将其换入蓄水池,如果换入则随机选择k个节点中的一个进行替换;同理第k+2个节点以k/(k+2)的概率决定是否将其换入蓄水池;直至最后节点,最终能够保证所有节点被选中的概率均为k/n,证明如下:

对于第m个节点,被选中的概率为:它被选中的概率 * (之后节点没被选中的概率 + 之后节点被选中但没有替换掉它的概率),即(k/m) * ((m+1-k)/(m+1) + (k/(m+1)) * ((k-1)/k)) * ... * ((n-k)/n + (k/n) * ((k-1)/k)) = k/n.

wiki上蓄水池抽样的页面,感兴趣的同学可以看下:https://en.wikipedia.org/wiki/Reservoir_sampling

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值