每日小结

秘书问题

  • 又称相亲问题、止步问题、见好就收问题、苏丹的嫁妆问题、挑剔的求婚者问题等

  • 要聘请一名秘书,有 n 个应聘者。每次面试一人,面试后就要及时决定是否聘他,如果当时决定不聘他,他便不会回来。面试后总能清楚了解应聘者的合适程度,并能和之前的每个人做比较。问什么样的策略,才使最佳人选被选中的概率最大。

  • 答案:

    展开
    • 这个问题的最优解是一个停止规则。在这个规则里,面试官会拒绝头 r - 1 个应聘者 (令他们中的最佳人选为 应聘者 M),然后选出第一个比 M 好的应聘者。可见最优策略包含于这个系列的策略中。 (如果M在所有n个应聘者中也是最好的一个,那么这个策略将选不出任何人选)对于任意的截断值 r,最佳人选被选中的概率是:

      P(r)=i=1nP(applicant i is selectedapplicant i is the best)=i=1nP(applicant i is selectedapplicant i is the best)P(applicant i is the best)=[i=1r10+i=rnP(the best of the first i1 applicantsis in the first r1 applicantsapplicant i is the best)]1n=[i=rnr1i1]1n=r1ni=rn1i1.{\displaystyle {\begin{aligned}P(r)&=\sum _{i=1}^{n}P\left({\text{applicant }}i{\text{ is selected}}\cap {\text{applicant }}i{\text{ is the best}}\right)\\&=\sum _{i=1}^{n}P\left({\text{applicant }}i{\text{ is selected}}|{\text{applicant }}i{\text{ is the best}}\right)\cdot P\left({\text{applicant }}i{\text{ is the best}}\right)\\&=\left[\sum _{i=1}^{r-1}0+\sum _{i=r}^{n}P\left(\left.{\begin{array}{l}{\text{the best of the first }}i-1{\text{ applicants}}\\{\text{is in the first }}r-1{\text{ applicants}}\end{array}}\right|{\text{applicant }}i{\text{ is the best}}\right)\right]\cdot {\frac {1}{n}}\\&=\left[\sum _{i=r}^{n}{\frac {r-1}{i-1}}\right]\cdot {\frac {1}{n}}\quad =\quad {\frac {r-1}{n}}\sum _{i=r}^{n}{\frac {1}{i-1}}.\end{aligned}}}

    • 当n趋近于无穷大时

      P(x)=xx11tdt=xln(x).{\displaystyle P(x)=x\int _{x}^{1}{\frac {1}{t}}\,dt=-x\ln(x).}

    • 求出最优的x值为

      1e\frac {1}{e}

Nginx 配置

  • root: 表示去哪个目录下寻找对应url的文件, 实际上是添加前缀

    1
    2
    3
    location /aaaa/ {
    root /home/tom/www/
    }
    • 请求: http://(hostname)/aaaa/hello.txt

    • 返回: /home/tom/www/aaaa/hello.txt

  • alias: 表示把匹配成功的路径替换成alias

    1
    2
    3
    location /aaaa/ {
    alias /home/tom/www/
    }
    • 请求: http://(hostname)/aaaa/hello.txt

    • 返回: /home/tom/www/hello.txt

  • PS: Nginx会自动将两个连续的斜杠替换成一个

Flask 嵌套路由配置

  • 使用register_blueprint时的url_prefix参数

    1
    2
    3
    # create app, blueprints, etc.
    app.register_blueprint(myblueprint, url_prefix='/somepath')
    # ...

视频网站设计思考

  • 视频文件存储到哪里: OSS对象存储服务

数据库隔离级别

  1. 读未提交(Read Uncommitted)

  2. 读已提交(Read Committed)/不可重复读 大多数数据库默认的隔离级别

  3. 可重复读(Repeatable-Read) mysql数据库所默认的级别

  4. 序列化(serializable)