LeetCode Weekly Contest 285

排名1515 / 7501。

并查集

labuladong

通用模板,路径压缩

应用场景

  • 克鲁斯卡尔最小生成树
    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
    template <class T>
    class UF {
    public:
    // n 为图中节点的个数
    explicit UF(int n)
    {
    count_ = n;
    parent_ = new T[n];
    for (T i = 0; i < n; i++) {
    parent_[i] = i;
    }
    }

    // 将节点 p 和节点 q 连通
    void Union(T p, T q) {
    T rootP = Find(p);
    T rootQ = Find(q);

    if (rootP == rootQ) {
    return;
    }

    parent_[rootQ] = rootP;
    // 两个连通分量合并成一个连通分量
    count_--;
    }

    // 判断节点 p 和节点 q 是否连通
    bool Connected(T p, T q) {
    T rootP = Find(p);
    T rootQ = Find(q);
    return rootP == rootQ;
    }

    // 返回图中的连通分量个数
    [[nodiscard]] int Count() const {
    return count_;
    }

    // 返回节点 x 的连通分量根节点
    private:
    T Find(T x) {
    while (parent_[x] != x) {
    // 进行路径压缩
    parent_[x] = parent_[parent_[x]];
    x = parent_[x];
    }
    return x;
    }
    // 连通分量个数
    int count_;
    // 存储每个节点的父节点
    T* parent_;
    };

990. 等式方程的可满足性

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution {
public:
bool equationsPossible(vector<string>& equations) {
int n = equations.size();
UF<int> uf(26);
for (const auto& eq :equations) {
if (eq.substr(1, 2) == "==") {
uf.Union(eq[0] - 'a', eq[3] - 'a');
}
}

for (const auto& eq :equations) {
if (eq.substr(1, 2) == "!=") {
if (uf.Connected(eq[0] - 'a', eq[3] - 'a')) {
return false;
}
}
}
return true;
}
};

岛屿问题

labuladong

200. 岛屿数量

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
class Solution {
public:
void dfs(vector<vector<char>>& grid, int i, int j, int m, int n) {
if (i < 0 || i > m - 1 || j < 0 || j > n - 1) {
return;
}
if (grid[i][j] == '0') {
return;
}
grid[i][j] = '0';
dfs(grid, i - 1, j, m, n);
dfs(grid, i + 1, j, m, n);
dfs(grid, i, j - 1, m, n);
dfs(grid, i, j + 1, m, n);
}
int numIslands(vector<vector<char>>& grid) {
int result = 0;
int m = grid.size();
int n= grid[0].size();
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (grid[i][j] == '1') {
result++;
dfs(grid, i, j, m, n);
}
}
}
return result;
}
};

1254. 统计封闭岛屿的数目

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
class Solution {
public:
void dfs(vector<vector<int>>& grid, int i, int j, int m, int n) {
if (i < 0 || i > m - 1 || j < 0 || j > n - 1) {
return;
}
if (grid[i][j] == 1) {
return;
}
grid[i][j] = 1;
dfs(grid, i - 1, j, m, n);
dfs(grid, i + 1, j, m, n);
dfs(grid, i, j - 1, m, n);
dfs(grid, i, j + 1, m, n);
}
int closedIsland(vector<vector<int>>& grid) {
int result = 0;
int m = grid.size();
int n = grid[0].size();
for (int i = 0; i < m; i++) {
dfs(grid, i, 0, m, n);
dfs(grid, i, n - 1, m, n);
}
for (int i = 0; i < n; i++) {
dfs(grid, 0, i, m, n);
dfs(grid, m - 1, i, m, n);
}
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (grid[i][j] == 0) {
result++;
dfs(grid, i, j, m, n);
}
}
}
return result;
}
};

1020. 飞地的数量

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
class Solution {
public:
void dfs(vector<vector<int>>& grid, int i, int j, int m, int n) {
if (i < 0 || i > m - 1 || j < 0 || j > n - 1) {
return;
}
if (grid[i][j] == 0) {
return;
}
grid[i][j] = 0;
dfs(grid, i - 1, j, m, n);
dfs(grid, i + 1, j, m, n);
dfs(grid, i, j - 1, m, n);
dfs(grid, i, j + 1, m, n);
}

int numEnclaves(vector<vector<int>>& grid) {
int result = 0;
int m = grid.size();
int n = grid[0].size();
for (int i = 0; i < m; i++) {
dfs(grid, i, 0, m, n);
dfs(grid, i, n - 1, m, n);
}
for (int i = 0; i < n; i++) {
dfs(grid, 0, i, m, n);
dfs(grid, m - 1, i, m, n);
}
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
result += (grid[i][j] == 1);
}
}
return result;
}
};

695. 岛屿的最大面积

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 {
public:
int maxArea = 0;
int dfs(vector<vector<int>>& grid, int i, int j, int m, int n) {
if (i < 0 || i > m - 1 || j < 0 || j > n - 1) {
return 0;
}
if (grid[i][j] == 0) {
return 0;
}
grid[i][j] = 0;
return dfs(grid, i - 1, j, m, n) + dfs(grid, i + 1, j, m, n) + dfs(grid, i, j - 1, m, n) + dfs(grid, i, j + 1, m, n) + 1;
}

int maxAreaOfIsland(vector<vector<int>>& grid) {
int m = grid.size();
int n = grid[0].size();
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (grid[i][j] == 1) {
maxArea = max(maxArea, dfs(grid, i, j, m, n));
}
}
}
return maxArea;
}
};

1905. 统计子岛屿

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
class Solution {
public:
bool dfs(const vector<vector<int>>& grid1, vector<vector<int>>& grid2, int i, int j, int m, int n) {
if (i < 0 || i > m - 1 || j < 0 || j > n - 1) {
return true;
}
if (grid2[i][j] == 0) {
return true;
}
if (grid1[i][j] == 0) {
return false;
}
grid2[i][j] = 0;
bool a = dfs(grid1, grid2, i - 1, j, m, n);
bool b = dfs(grid1, grid2, i + 1, j, m, n);
bool c = dfs(grid1, grid2, i, j - 1, m, n);
bool d = dfs(grid1, grid2, i, j + 1, m, n);
return a && b && c && d;
}

int countSubIslands(vector<vector<int>>& grid1, vector<vector<int>>& grid2) {
int result = 0;
int m = grid1.size();
int n = grid1[0].size();
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (grid1[i][j] && grid2[i][j]) {
result += dfs(grid1, grid2, i, j, m, n);
}
}
}
return result;
}
};

694. 不同的岛屿数量

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
int numDistinctIslands(int[][] grid) {

int m = grid.length, n = grid[0].length;

// 记录所有岛屿的序列化结果

HashSet<String> islands = new HashSet<>();

for (int i = 0; i < m; i++) {

for (int j = 0; j < n; j++) {

if (grid[i][j] == 1) {

// 淹掉这个岛屿,同时存储岛屿的序列化结果

StringBuilder sb = new StringBuilder();

// 初始的方向可以随便写,不影响正确性

dfs(grid, i, j, sb, 666);

islands.add(sb.toString());

}

}

}

// 不相同的岛屿数量

return islands.size();

}

LeetCode biweekly-contest-68

排名1143 / 2854

LeetCode Weekly Contest 272

前三道题很简单,半个小时完成,最后一道题着实有点难,思路都错了,最后排名1695 / 4697。

LeetCode Weekly Contest 271

这次的题挺简单的,早上起这么早,困的不行下去买了杯瑞幸,喝的好腻犯恶心,状态这么差还是半个小时就写完3道题了,最后一道题差一点就写出来了,走去聚餐的路上想到思路了,最后排名1087 / 4561。可惜,差点达成第一次ak的操作,下次一定!

LeetCode biweekly-contest-67

今天加班了一天,回家已经好累了,洗个澡精神一下继续冲题了,不知道是不是题目比较简单还是自己提高了,状态这么差每道题还都有思路,第二道题和第四道题超时了,实在没脑子了,第一次进1000,排名711 / 2923

LeetCode Weekly Contest 269

这次的题还是挺简单的,还是做出来3道题,最后一道题审题失误,先写了一份回头就来不及了,思路还是有的,排名2017 / 4292,提交错误被罚时,导致排名比上周下降了

LeetCode biweekly-contest-66

晚上唱K失败,团队唱K热情不足呀,还是乖乖回来刷题吧,这次前两题轻松拿下,第三道题DFS又超时,我想了好久的剪枝都没想出来,没想到其实每个格子的权重并不是都不一样,最后一道题就没看了,排名1322 / 2803,唉,一直都是average的水平

LeetCode Weekly Contest 268

这次的题感觉还是挺简单的,做出来3道题,但是第一道题浪费太多时间了,排名1242 / 4397,进步很大,继续努力

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×