力扣第305场周赛复盘

警告
本文最后更新于 2022-08-07,文中内容可能已过时。

第 304 场周赛

  • 全国排名:556 / 7426(8.08%)
  • 全球排名:1309 / 26877(4.64%)
  • 分数变化:2095 + 17 = 2112
  • 完成时间:42m20s(+5m)
  • 题目1
    • 难度:
    • 顺序:1
    • 用时:5m46s
    • 错误:0
  • 题目2
    • 难度:
    • 顺序:2
    • 用时:7m28s
    • 错误:0
  • 题目3
    • 难度:
    • 顺序:3
    • 用时:16m28s
    • 错误:1
      • dp 初始化错误
  • 题目4
    • 难度:
    • 顺序:4
    • 用时:17m38s
    • 错误:0

6136. 算术三元组的数目

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
class Solution {
    public int arithmeticTriplets(int[] nums, int diff) {
        HashMap<Integer, Integer> map = new HashMap<>();
        int n = nums.length;
        for (int i = 0; i < n; i++) map.put(nums[i], i);
        int ans = 0;
        for (int i = 1; i + 1 < n; i++) {
            int prev = map.getOrDefault(nums[i] - diff, n);
            int next = map.getOrDefault(nums[i] + diff, -1);
            if (prev < i && next > i) ans++;
        }
        return ans;
    }
}

6139. 受限条件下可到达节点的数目

 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
class Solution {
    int ans = 0;

    public int reachableNodes(int n, int[][] edges, int[] restricted) {
        List<Integer>[] graph = new ArrayList[n];
        for (int i = 0; i < n; i++)
            graph[i] = new ArrayList<>();
        for (int[] edge : edges) {
            graph[edge[0]].add(edge[1]);
            graph[edge[1]].add(edge[0]);
        }
        HashSet<Integer> res = new HashSet<>();
        for (int x : restricted) res.add(x);
        boolean[] vis = new boolean[n];
        dfs(graph, 0, res, vis);
        return ans;
    }

    void dfs(List<Integer>[] graph, int u, HashSet<Integer> res, boolean[] vis) {
        vis[u] = true;
        ans++;
        for (int v : graph[u]) {
            if (!res.contains(v) && !vis[v])
                dfs(graph, v, res, vis);
        }
    }
}

6137. 检查数组是否存在有效划分

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
class Solution {
    public boolean validPartition(int[] nums) {
        int n = nums.length;
        // dp[i] 表示前 i 个元素是否存在有效划分
        boolean[] dp = new boolean[n + 1];
        dp[0] = true;
        dp[2] = nums[0] == nums[1];
        for (int i = 2; i < n; i++) {
            dp[i + 1] = dp[i - 1] && nums[i] == nums[i - 1] ||
                        dp[i - 2] && nums[i] == nums[i - 1] && nums[i] == nums[i - 2] ||
                        dp[i - 2] && nums[i] - 1 == nums[i - 1] && nums[i] - 2 == nums[i - 2];
        }
        return dp[n];
    }
}

6138. 最长理想子序列

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
class Solution {
    public int longestIdealString(String s, int k) {
        int[] dp = new int[26];
        for (char c : s.toCharArray()) {
            int x = c - 'a';
            int maxLen = 0;
            for (int i = Math.max(0, x - k); i <= Math.min(25, x + k); i++)
                maxLen = Math.max(maxLen, dp[i]);
            d[x] = maxLen + 1;
        }
        int ans = 0;
        for (int x : dp) ans = Math.max(ans, x);
        return ans;
    }
}
  • 考察 dp