boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 问一道题,老题不过找不到答案
相关主题
bloomberge phone interview question
问一个老题
问两个GG老题
请教一道产生随机数的问题
再问一道老题
问10个老题
9球找一个不一样重量的
求教一道老题
一道老题但是以前的解好象都不对
一道老题,突然想不起来怎么做了
相关话题的讨论汇总
话题: line话题: chance话题: memory话题: read话题: nth
进入JobHunting版参与讨论
1 (共1页)
o**s
发帖数: 65
1
一个文件的内容行数未知, 要一次扫描, 任意输出一行.
要求输出每一行的概率相等. 内存很小, 最多只能放下一行的内容.
如果可以扫描两次, 第一次得到行数, 很简单.
如果只扫描一次呢?
a*******8
发帖数: 2299
2
一个counter记录行数,每次取0-1随机数,
如果小于1/counter就记录当前行,否则skip当前行。
x*****p
发帖数: 1707
3
Read first line, keep it in memory;
Read second line, keep it in memory with 1/2 chance
...
Read nth line, keep it in memory with 1/n chance.
Therefore, the chance that the k-th line is kept in the memory is
(1 - 1/2)(1 - 1/3)(1 - 1/4)...(1 - 1/k) = 1/k
s*****n
发帖数: 5488
4
这个是正解,就是英文没说清楚让我想了半天。
应该是。
内存中留一行作为buffer.
从第二行开始,以1/n概率replace内存中那一行。

【在 x*****p 的大作中提到】
: Read first line, keep it in memory;
: Read second line, keep it in memory with 1/2 chance
: ...
: Read nth line, keep it in memory with 1/n chance.
: Therefore, the chance that the k-th line is kept in the memory is
: (1 - 1/2)(1 - 1/3)(1 - 1/4)...(1 - 1/k) = 1/k

x******3
发帖数: 245
5

the chance that the k-th line is kept in the memory is
(1/k)(1-1/(k+1))(1-1/(k+2))...(1-1/n) = 1/n
so any line is chosen with the chance of 1/n

【在 x*****p 的大作中提到】
: Read first line, keep it in memory;
: Read second line, keep it in memory with 1/2 chance
: ...
: Read nth line, keep it in memory with 1/n chance.
: Therefore, the chance that the k-th line is kept in the memory is
: (1 - 1/2)(1 - 1/3)(1 - 1/4)...(1 - 1/k) = 1/k

j**l
发帖数: 2911
6
reservior sampling?
x****k
发帖数: 2932
x****k
发帖数: 2932
8
方法是对的,但公式可能不对,应该是
1/k *(1-1/(k+1))*(1-1/(k+2))*...*(1-1/(n)) = 1/n
对任意the k-nth line,the probability are all 1/n

【在 x*****p 的大作中提到】
: Read first line, keep it in memory;
: Read second line, keep it in memory with 1/2 chance
: ...
: Read nth line, keep it in memory with 1/n chance.
: Therefore, the chance that the k-th line is kept in the memory is
: (1 - 1/2)(1 - 1/3)(1 - 1/4)...(1 - 1/k) = 1/k

1 (共1页)
进入JobHunting版参与讨论
相关主题
一道老题,突然想不起来怎么做了
问一道老题
问一个老题 longest palindrome
一道老题
问一个老题,请帮忙解答 多谢了
问到经典老题
确认一下RMQ/LCA那道老题
求教一道老题
一道老题
careercup书上一个老题
相关话题的讨论汇总
话题: line话题: chance话题: memory话题: read话题: nth