Contents

ARST打卡第333周

Algorithm

lc1912_设计电影租借系统

思路:

其实主要是2个优先队列维护好数据即可,也可以看作是排序数据库设计题。

看题解学习一下: 题解细节

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
use std::collections::{HashMap, BTreeSet};
use std::cmp::Ordering;

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
struct Pair {
    first: i32,
    second: i32,
}

impl Pair {
    fn new(first: i32, second: i32) -> Self {
        Pair { first, second }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
struct Triple {
    price: i32,
    shop: i32,
    movie: i32,
}

impl Triple {
    fn new(price: i32, shop: i32, movie: i32) -> Self {
        Triple { price, shop, movie }
    }
}

struct MovieRentingSystem {
    t_price: HashMap<Pair, i32>,
    t_valid: HashMap<i32, BTreeSet<Pair>>,
    t_rent: BTreeSet<Triple>,
}

impl MovieRentingSystem {
    fn new(n: i32, entries: Vec<Vec<i32>>) -> Self {
        let mut t_price = HashMap::new();
        let mut t_valid = HashMap::new();
        
        for entry in entries {
            let shop = entry[0];
            let movie = entry[1];
            let price = entry[2];
            t_price.insert(Pair::new(shop, movie), price);
            t_valid.entry(movie)
                .or_insert_with(BTreeSet::new)
                .insert(Pair::new(price, shop));
        }
        
        MovieRentingSystem {
            t_price,
            t_valid,
            t_rent: BTreeSet::new(),
        }
    }
    
    fn search(&self, movie: i32) -> Vec<i32> {
        self.t_valid.get(&movie)
            .map_or_else(Vec::new, |set| {
                set.iter()
                    .take(5)
                    .map(|p| p.second)
                    .collect()
            })
    }
    
    fn rent(&mut self, shop: i32, movie: i32) {
        let price = self.t_price[&Pair::new(shop, movie)];
        self.t_valid.get_mut(&movie).unwrap().remove(&Pair::new(price, shop));
        self.t_rent.insert(Triple::new(price, shop, movie));
    }
    
    fn drop(&mut self, shop: i32, movie: i32) {
        let price = self.t_price[&Pair::new(shop, movie)];
        self.t_valid.get_mut(&movie).unwrap().insert(Pair::new(price, shop));
        self.t_rent.remove(&Triple::new(price, shop, movie));
    }
    
    fn report(&self) -> Vec<Vec<i32>> {
        self.t_rent.iter()
            .take(5)
            .map(|t| vec![t.shop, t.movie])
            .collect()
    }
}

Review

婚姻的历史【TED-Ed】

Tip

Binance创始人CZ详谈BNB、BNBChain及其生态

CZ:这个趋势⾮常清晰。更远的未来,DEX 会⽐ CEX 更⼤,这点⾮常明确。正如你说的,CEX 在我看来是⼈们进⼊加密世界的「垫脚⽯」。来⾃ Web2 的⽤户,会觉得⽤邮箱和密码更容易上⼿,有客服、有⼈可以⼿把⼿帮你。托管平台的概念也更容易理 解,因为它在概念上更像银⾏。但随着他们更有经验,就会说:我现在有⾃⼰的钱包,可以⾃⼰管理,这给了我更⾼⾃由度和控制权,也意味着更多责任(⽐如要保护好设 备)。⼀旦⼈们掌握了这些,就会转向 DEX。 CZ:因此我百分之百相信,未来 DEX 会⽐ CEX 更⼤。由此,链本⾝ — —链的⽣态 — —就⾮常重要。这也是为什么在我看来,从⻓期看,「链的⽣态」远⽐任何⼀家中⼼化交 易所更重要。从这个意义上说,我被迫不能再花时间在中⼼化交易平台上,反⽽是好事。现在我可以有更多时间为去中⼼化⽣态做贡献。⽽且这其实挺迷⼈的,因为⼀旦你习 惯了它……我会说,当下对普通⼈⽽⾔,去中⼼化⽣态仍然⽐较难⽤。交易量在增⻓,也相当可观,但对普通⽤户来说,使⽤去中⼼化产品会看到很多随机字符串、很多屏 幕上的随机数字 — —该怎么做?即便在中⼼化交易平台,也有很多数字,但⾄少是能理 解的。我们需要作为⼀个社区把产品做得更好、更易⽤。但「在链上、去中⼼化」肯定是未来;否则我们也不会在这个⾏业⾥,对吧?

Share

顺道者胜实践感悟

  1. 顺应趋势,不要逆势而为。
  2. 如果发现自己逆势而为了,立马改正错误可能会付出最小的代价。
  3. 如果发现已经很难沉没成本,并且最坏结果能抗住,并且有道反的可能,就坚持到道反的时候改正错误。