Contents

ARST打卡第306周

Algorithm

lc2272_最大波动的子字符串

思路:

这里属于是找双字符数量差最大的子串,然后返回数量差。不过这里子串不一定要只有两种字符。

串长 1e4, 但是总之要遍历所有子串+剪枝来找到最大值。

试了一下 claude-3.7-sonnet-thinking,反复给出答案,反复承认自己第一版(claude-3.7-sonnet)错了,不断优化,厉害。

 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
impl Solution {
    pub fn largest_variance(s: String) -> i32 {
        let s_bytes = s.as_bytes();
        let mut result = 0;
        
        // 遍历所有可能的字符对
        for i in 0..26 {
            for j in 0..26 {
                if i == j {
                    continue;
                }
                
                let a = b'a' + i as u8;
                let b = b'a' + j as u8;
                
                // 基本 Kadane 算法
                let mut cur = 0;
                let mut has_b = false;
                
                for &c in s_bytes {
                    if c == a {
                        cur += 1;
                    } else if c == b {
                        cur -= 1;
                        has_b = true;
                    }
                    
                    if has_b {
                        result = result.max(cur);
                    }
                    
                    if cur < 0 {
                        cur = 0;
                        has_b = false;
                    }
                }
                
                // 反向遍历,确保我们不会错过某些情况
                cur = 0;
                has_b = false;
                
                for &c in s_bytes.iter().rev() {
                    if c == a {
                        cur += 1;
                    } else if c == b {
                        cur -= 1;
                        has_b = true;
                    }
                    
                    if has_b {
                        result = result.max(cur);
                    }
                    
                    if cur < 0 {
                        cur = 0;
                        has_b = false;
                    }
                }
            }
        }
        
        result
    }
}

题解的动态规划+hash字符的索引数组更是出神入化,学习。

 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
use std::collections::HashMap;
use std::cmp::{max, min};

impl Solution {
    pub fn largest_variance(s: String) -> i32 {
        let mut pos: HashMap<char, Vec<usize>> = HashMap::new();
        for (i, ch) in s.char_indices() {
            pos.entry(ch).or_insert(Vec::new()).push(i);
        }
        let mut ans = 0;
        for (&c0, pos0) in &pos {
            for (&c1, pos1) in &pos {
                if c0 != c1 {
                    let mut i = 0;
                    let mut j = 0;
                    let mut f = 0;
                    let mut g = i32::MIN;
                    while i < pos0.len() || j < pos1.len() {
                        if j == pos1.len() || (i < pos0.len() && pos0[i] < pos1[j]) {
                            f = max(f, 0) + 1;
                            g = g + 1;
                            i += 1;
                        } else {
                            g = max(f, max(g, 0)) - 1;
                            f = max(f, 0) - 1;
                            j += 1;
                        }
                        ans = max(ans, g);
                    }
                }
            }
        }
        ans
    }
}

Review

你有没有冒充者综合症,这也是优势?【TED演讲】

这个没有字幕,然后大部分没听懂,还得多练,哈哈

人们经常觉得自己德不配位,但其实很多人都这样觉得。

接受自己的不确定性,并勇敢走出舒适区,持续学习和进步,以此克服内心的冒充者综合征,实现更大突破。

Tips

浏览 github trending 看到 SpacetimeDB ,推荐可以了解一下.

这里 SpacetimeDB 的交互方式其实和 区块链 的 dapp 很像,读取可以直接读DB,然后交互dapp发给server。 它自己也说了:

It’s actually similar to the idea of smart contracts, except that SpacetimeDB is a database, has nothing to do with blockchain, and is orders of magnitude faster than any smart contract system.

不过这个数据库确实挺差异化竞争的:

SpacetimeDB is optimized for maximum speed and minimum latency rather than batch processing or OLAP workloads. It is designed to be used for real-time applications like games, chat, and collaboration tools.

This speed and latency is achieved by holding all of application state in memory, while persisting the data in a write-ahead-log (WAL) which is used to recover application state.

Share

为什么购买cursor pro年度版:

  1. 最近把运气投资赚的钱凭借自己的努力亏完了,感觉不用省小钱,Die With Zero,改善生活不影响资金总量的。
  2. Cursor 对比 VScode + copilot:
    1. 对于上下文选择支持更好
    2. 对 vim 支持更好,不会拼音输入抖动
  3. 为啥不试用: 之前试用很爽,但是最近试用版本老是用不上 Claude 3.7 sonnet thinking, 会浪费很多时间。