Contents

ARST打卡第312周

Algorithm

lc2444_统计定界子数组的数目

思路:

暴力解法需要遍历所有的子数组,然后判断数组里面的最大最小是否符合要求,时间复杂度为O(n^3)

没想到好思路,然后学习了题解

根据题意可知,任何一个定界子数组都不会包含大于 maxK 或者小于 minK 的数字,因此我们可以用这些数将原数组分割成若干段,每个段内只包含数值在 [minK,maxK] 之间的数字。

对于每个段,我们记 border 为段左边界(为了方便计算,令第 border+1 个数才是段内的第一个数字),然后从左到右遍历时,记当前位置 i 左侧第一个数值等于 minK 的位置是 last_min,左侧第一个数值等于 maxK 的位置是 last_max,那么在这个段内,以 i 为右端点的定界子数组共有 min(last_min,last_max)−border 个(可以理解为对应左端点个数)。

我们遍历完所有段,将所有以 i 为右端点的定界子数组个数加起来就是答案。

 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
impl Solution {
    pub fn count_subarrays(nums: Vec<i32>, min_k: i32, max_k: i32) -> i64 {
        let mut res = 0i64;
        let mut border = -1;
        let mut last_min = -1;
        let mut last_max = -1;
        for i in 0..nums.len() {
            let num = nums[i];
            if num < min_k || num > max_k {
                last_max = -1;
                last_min = -1;
                border = i as i32;
            }
            if num == min_k {
                last_min = i as i32;
            }
            if num == max_k {
                last_max = i as i32;
            }
            if last_min != -1 && last_max != -1 {
                res += (last_min.min(last_max) - border) as i64;
            }
        }
        res
    }
}

Review

人类该如何在一个世纪内使预期寿命翻倍?【TED演讲】

从平均不到45岁到平均大于70岁,主要是通过疫苗和抗生素等科技的进步。

呼吁关注更多不发展地区的健康,而不是关注无限寿命。

没字幕还是有点难一直连续听懂,继续努力

Tip

许式伟回金山交流go+Mcp

Share

cursor使用小技巧

面对超大代码项目的时候,cursor问答往往得不到好的结果。

因为可以尝试以下操作来获得更好的结果:

  1. 开启 index 全部项目文件 (cursors settings -> features -> Codebase indexing)
  2. 开启长上下文功能 (cursors settings -> features -> Chat -> Large context) 可能多花请求数,但是值的
  3. 使用 cursor agent (cmd+i) 要求阅读指定的文件