Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b1b3576

Browse files
feat: add solutions to lc problem: No.0684 (doocs#3675)
No.0684.Redundant Connection
1 parent 7210905 commit b1b3576

File tree

15 files changed

+1043
-136
lines changed

15 files changed

+1043
-136
lines changed

‎solution/0600-0699/0684.Redundant Connection/README.md‎

Lines changed: 363 additions & 43 deletions
Large diffs are not rendered by default.

‎solution/0600-0699/0684.Redundant Connection/README_EN.md‎

Lines changed: 363 additions & 43 deletions
Large diffs are not rendered by default.
Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
11
class Solution {
22
public:
3-
vector<int> p;
4-
53
vector<int> findRedundantConnection(vector<vector<int>>& edges) {
6-
p.resize(1010);
7-
for (int i = 0; i < p.size(); ++i) p[i] = i;
8-
for (auto& e : edges) {
9-
int a = e[0], b = e[1];
10-
if (find(a) == find(b)) return e;
11-
p[find(a)] = find(b);
4+
int n = edges.size();
5+
vector<int> p(n);
6+
iota(p.begin(), p.end(), 0);
7+
function<int(int)> find = [&](int x) {
8+
return x == p[x] ? x : p[x] = find(p[x]);
9+
};
10+
for (int i = 0;; ++i) {
11+
int pa = find(edges[i][0] - 1);
12+
int pb = find(edges[i][1] - 1);
13+
if (pa == pb) {
14+
return edges[i];
15+
}
16+
p[pa] = pb;
1217
}
13-
return {};
1418
}
15-
16-
int find(int x) {
17-
if (p[x] != x) p[x] = find(p[x]);
18-
return p[x];
19-
}
20-
};
19+
};
Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
func findRedundantConnection(edges [][]int) []int {
2-
p := make([]int, 1010)
2+
n := len(edges)
3+
p := make([]int, n)
34
for i := range p {
45
p[i] = i
56
}
6-
var find func(xint) int
7+
var find func(int) int
78
find = func(x int) int {
89
if p[x] != x {
910
p[x] = find(p[x])
1011
}
1112
return p[x]
1213
}
13-
for _, e:= rangeedges {
14-
a, b := e[0], e[1]
15-
if find(a) == find(b) {
16-
return e
14+
for i:= 0; ; i++ {
15+
pa, pb := find(edges[i][0]-1), find(edges[i][1]-1)
16+
if pa == pb {
17+
return edges[i]
1718
}
18-
p[find(a)] = find(b)
19+
p[pa] = pb
1920
}
20-
return []int{}
21-
}
21+
}

‎solution/0600-0699/0684.Redundant Connection/Solution.java‎

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,19 @@ class Solution {
22
private int[] p;
33

44
public int[] findRedundantConnection(int[][] edges) {
5-
p = new int[1010];
6-
for (int i = 0; i < p.length; ++i) {
5+
int n = edges.length;
6+
p = new int[n];
7+
for (int i = 0; i < n; ++i) {
78
p[i] = i;
89
}
9-
for (int[] e : edges) {
10-
int a = e[0], b = e[1];
11-
if (find(a) == find(b)) {
12-
return e;
10+
for (int i = 0;; ++i) {
11+
int pa = find(edges[i][0] - 1);
12+
int pb = find(edges[i][1] - 1);
13+
if (pa == pb) {
14+
return edges[i];
1315
}
14-
p[find(a)] = find(b);
16+
p[pa] = pb;
1517
}
16-
return null;
1718
}
1819

1920
private int find(int x) {
@@ -22,4 +23,4 @@ private int find(int x) {
2223
}
2324
return p[x];
2425
}
25-
}
26+
}

‎solution/0600-0699/0684.Redundant Connection/Solution.js‎

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,20 @@
33
* @return {number[]}
44
*/
55
var findRedundantConnection = function (edges) {
6-
let p = Array.from({ length: 1010 }, (_, i) => i);
7-
function find(x) {
8-
if (p[x] != x) {
6+
const n = edges.length;
7+
const p = Array.from({ length: n }, (_, i) => i);
8+
const find = x => {
9+
if (p[x] !== x) {
910
p[x] = find(p[x]);
1011
}
1112
return p[x];
12-
}
13-
for (let [a, b] of edges) {
14-
if (find(a) == find(b)) {
15-
return [a, b];
13+
};
14+
for (let i = 0; ; ++i) {
15+
const pa = find(edges[i][0] - 1);
16+
const pb = find(edges[i][1] - 1);
17+
if (pa === pb) {
18+
return edges[i];
1619
}
17-
p[find(a)] = find(b);
20+
p[pa] = pb;
1821
}
19-
return [];
2022
};
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
class Solution:
22
def findRedundantConnection(self, edges: List[List[int]]) -> List[int]:
3-
def find(x):
3+
def find(x: int) ->int:
44
if p[x] != x:
55
p[x] = find(p[x])
66
return p[x]
77

8-
p = list(range(1010))
8+
p = list(range(len(edges)))
99
for a, b in edges:
10-
if find(a) == find(b):
10+
pa, pb = find(a - 1), find(b - 1)
11+
if pa == pb:
1112
return [a, b]
12-
p[find(a)] = find(b)
13-
return []
13+
p[pa] = pb
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
function findRedundantConnection(edges: number[][]): number[] {
2+
const n = edges.length;
3+
const p: number[] = Array.from({ length: n }, (_, i) => i);
4+
const find = (x: number): number => {
5+
if (p[x] !== x) {
6+
p[x] = find(p[x]);
7+
}
8+
return p[x];
9+
};
10+
for (let i = 0; ; ++i) {
11+
const pa = find(edges[i][0] - 1);
12+
const pb = find(edges[i][1] - 1);
13+
if (pa === pb) {
14+
return edges[i];
15+
}
16+
p[pa] = pb;
17+
}
18+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
class UnionFind {
2+
public:
3+
UnionFind(int n) {
4+
p = vector<int>(n);
5+
size = vector<int>(n, 1);
6+
iota(p.begin(), p.end(), 0);
7+
}
8+
9+
bool unite(int a, int b) {
10+
int pa = find(a), pb = find(b);
11+
if (pa == pb) {
12+
return false;
13+
}
14+
if (size[pa] > size[pb]) {
15+
p[pb] = pa;
16+
size[pa] += size[pb];
17+
} else {
18+
p[pa] = pb;
19+
size[pb] += size[pa];
20+
}
21+
return true;
22+
}
23+
24+
int find(int x) {
25+
if (p[x] != x) {
26+
p[x] = find(p[x]);
27+
}
28+
return p[x];
29+
}
30+
31+
private:
32+
vector<int> p, size;
33+
};
34+
35+
class Solution {
36+
public:
37+
vector<int> findRedundantConnection(vector<vector<int>>& edges) {
38+
UnionFind uf(edges.size());
39+
for (int i = 0;; ++i) {
40+
if (!uf.unite(edges[i][0] - 1, edges[i][1] - 1)) {
41+
return edges[i];
42+
}
43+
}
44+
}
45+
};
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
type unionFind struct {
2+
p, size []int
3+
}
4+
5+
func newUnionFind(n int) *unionFind {
6+
p := make([]int, n)
7+
size := make([]int, n)
8+
for i := range p {
9+
p[i] = i
10+
size[i] = 1
11+
}
12+
return &unionFind{p, size}
13+
}
14+
15+
func (uf *unionFind) find(x int) int {
16+
if uf.p[x] != x {
17+
uf.p[x] = uf.find(uf.p[x])
18+
}
19+
return uf.p[x]
20+
}
21+
22+
func (uf *unionFind) union(a, b int) bool {
23+
pa, pb := uf.find(a), uf.find(b)
24+
if pa == pb {
25+
return false
26+
}
27+
if uf.size[pa] > uf.size[pb] {
28+
uf.p[pb] = pa
29+
uf.size[pa] += uf.size[pb]
30+
} else {
31+
uf.p[pa] = pb
32+
uf.size[pb] += uf.size[pa]
33+
}
34+
return true
35+
}
36+
37+
func findRedundantConnection(edges [][]int) []int {
38+
uf := newUnionFind(len(edges))
39+
for i := 0; ; i++ {
40+
if !uf.union(edges[i][0]-1, edges[i][1]-1) {
41+
return edges[i]
42+
}
43+
}
44+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /