Movatterモバイル変換


[0]ホーム

URL:


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

Commitb2f958f

Browse files
committed
684-redundant-connection.md Perfected UnionFind solution.
1 parent2689bea commitb2f958f

File tree

2 files changed

+186
-136
lines changed

2 files changed

+186
-136
lines changed

‎en/1-1000/684-redundant-connection.md

Lines changed: 93 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,12 @@ Output: [1,4]
4848
-`UnionFind` algorithm typically has three methods:
4949
- The`unite(node1, node2)` operation is used to merge two trees.
5050
- The`find_root(node)` method is used to return the root of a node.
51-
- The`same_root(node1, node2) == true` method is used to determine whether two nodes are in the same tree.
51+
- The`is_same_root(node1, node2) == true` method is used to determine whether two nodes are in the same tree.
5252

5353
##Approach (UnionFind algorithm)
5454
1. Initially, each node is in its own group.
5555
1. Iterate`edges` data and`unite(node1, node2)`.
56-
1. As soon as`same_root(node1, node2) == true` (a cycle will be formed), return`[node1, node2]`.
56+
1. As soon as`is_same_root(node1, node2) == true` (a cycle will be formed), return`[node1, node2]`.
5757

5858
##Complexity
5959
* Time:`O(n)`.
@@ -62,14 +62,11 @@ Output: [1,4]
6262
##Python
6363
```python
6464
classSolution:
65-
def__init__(self):
66-
self.parent=None
67-
6865
deffindRedundantConnection(self,edges: List[List[int]]) -> List[int]:
69-
self.parent=list(range(len(edges)+1))
66+
self.parents=list(range(len(edges)+1))
7067

7168
for x, yin edges:
72-
ifself.same_root(x, y):
69+
ifself.is_same_root(x, y):
7370
return [x, y]
7471

7572
self.unite(x, y)
@@ -78,34 +75,38 @@ class Solution:
7875
root_x=self.find_root(x)
7976
root_y=self.find_root(y)
8077

81-
self.parent[root_y]= root_x# Error-prone point
78+
self.parents[root_y]= root_x# Error-prone point 1
8279

8380
deffind_root(self,x):
84-
if x==self.parent[x]:
81+
parent=self.parents[x]
82+
83+
if x== parent:
8584
return x
8685

87-
self.parent[x]=self.find_root(self.parent[x])
86+
root=self.find_root(parent)# Error-prone point 2
8887

89-
returnself.parent[x]
88+
self.parents[x]= root# Error-prone point 3
89+
90+
return root
9091

91-
defsame_root(self,x,y):
92+
defis_same_root(self,x,y):
9293
returnself.find_root(x)==self.find_root(y)
9394
```
9495

9596
##Java
9697
```java
9798
classSolution {
98-
privateint[]parent;
99+
privateint[]parents;
99100

100101
publicint[]findRedundantConnection(int[][]edges) {
101-
parent=newint[edges.length+1];
102+
parents=newint[edges.length+1];
102103

103-
for (var i=0; i<parent.length; i++) {
104-
parent[i]= i;
104+
for (var i=0; i<parents.length; i++) {
105+
parents[i]= i;
105106
}
106107

107108
for (var edge: edges) {
108-
if (sameRoot(edge[0], edge[1])) {
109+
if (isSameRoot(edge[0], edge[1])) {
109110
return edge;
110111
}
111112

@@ -119,20 +120,24 @@ class Solution {
119120
int rootX= findRoot(x);
120121
int rootY= findRoot(y);
121122

122-
parent[rootY]= rootX;// Error-prone point 1
123+
parents[rootY]= rootX;// Error-prone point 1
123124
}
124125

125126
privateintfindRoot(intx) {
126-
if (x== parent[x]) {
127+
var parent= parents[x];
128+
129+
if (x== parent) {
127130
return x;
128131
}
129132

130-
parent[x]= findRoot(parent[x]);// Error-prone point 2
133+
var root= findRoot(parent);// Error-prone point 2
131134

132-
return parent[x];
135+
parents[x]= root;// Error-prone point 3
136+
137+
return root;
133138
}
134139

135-
privatebooleansameRoot(intx,inty) {
140+
privatebooleanisSameRoot(intx,inty) {
136141
return findRoot(x)== findRoot(y);
137142
}
138143
}
@@ -144,11 +149,11 @@ class Solution {
144149
public:
145150
vector<int> findRedundantConnection(vector<vector<int>>& edges) {
146151
for (auto i = 0; i <= edges.size(); i++) {
147-
parent.push_back(i);
152+
parents.push_back(i);
148153
}
149154

150155
for (auto& edge : edges) {
151-
if (sameRoot(edge[0], edge[1])) {
156+
if (isSameRoot(edge[0], edge[1])) {
152157
return edge;
153158
}
154159

@@ -159,70 +164,78 @@ public:
159164
}
160165

161166
private:
162-
vector<int>parent;
167+
vector<int>parents;
163168

164169
void unite(int x, int y) {
165170
int root_x = findRoot(x);
166171
int root_y = findRoot(y);
167172

168-
parent[root_y] = root_x; // Error-prone point 1
173+
parents[root_y] = root_x; // Error-prone point 1
169174
}
170175

171176
int findRoot(int x) {
172-
if (x == parent[x]) {
177+
auto parent = parents[x];
178+
179+
if (x == parent) {
173180
return x;
174181
}
175182

176-
parent[x]= findRoot(parent[x]); // Error-prone point 2
183+
auto root= findRoot(parent); // Error-prone point 2
177184

178-
return parent[x];
185+
parents[x] = root; // Error-prone point 3
186+
187+
return root;
179188
}
180189

181-
boolsameRoot(int x, int y) {
190+
boolisSameRoot(int x, int y) {
182191
return findRoot(x) == findRoot(y);
183192
}
184193
};
185194
```
186195
187196
## JavaScript
188197
```javascript
189-
letparent
198+
letparents
190199
191200
var findRedundantConnection = function(edges) {
192-
parent = []
201+
parents = []
193202
for (let i = 0; i <= edges.length; i++) {
194-
parent.push(i)
203+
parents.push(i)
195204
}
196205
197206
for (let [x, y] of edges) {
198-
if (sameRoot(x, y)) {
207+
if (isSameRoot(x, y)) {
199208
return [x, y]
200209
}
201210
202211
unite(x, y)
203212
}
204213
205-
returnsameRoot(source, destination)
214+
returnisSameRoot(source, destination)
206215
};
207216
208217
function unite(x, y) {
209218
rootX = findRoot(x)
210219
rootY = findRoot(y)
211220
212-
parent[rootY] = rootX // Error-prone point 1
221+
parents[rootY] = rootX // Error-prone point 1
213222
}
214223
215224
function findRoot(x) {
216-
if (x == parent[x]) {
225+
const parent = parents[x]
226+
227+
if (x == parent) {
217228
return x
218229
}
219230
220-
parent[x]= findRoot(parent[x]) // Error-prone point 2
231+
const root= findRoot(parent) // Error-prone point 2
221232
222-
return parent[x]
233+
parents[x] = root // Error-prone point 3
234+
235+
return root
223236
}
224237
225-
functionsameRoot(x, y) {
238+
functionisSameRoot(x, y) {
226239
return findRoot(x) == findRoot(y)
227240
}
228241
```
@@ -231,18 +244,18 @@ function sameRoot(x, y) {
231244
```c#
232245
publicclassSolution
233246
{
234-
int[]parent;
247+
int[]parents;
235248

236249
publicint[]FindRedundantConnection(int[][]edges)
237250
{
238-
parent=newint[edges.Length+1];
251+
parents=newint[edges.Length+1];
239252

240-
for (inti=0;i<parent.Length;i++)
241-
parent[i]=i;
253+
for (inti=0;i<parents.Length;i++)
254+
parents[i]=i;
242255

243256
foreach (int[]edgeinedges)
244257
{
245-
if (sameRoot(edge[0],edge[1]))
258+
if (isSameRoot(edge[0],edge[1]))
246259
{
247260
returnedge;
248261
}
@@ -258,20 +271,24 @@ public class Solution
258271
introotX=findRoot(x);
259272
introotY=findRoot(y);
260273

261-
parent[rootY]=rootX;// Error-prone point 1
274+
parents[rootY]=rootX;// Error-prone point 1
262275
}
263276

264277
intfindRoot(intx)
265278
{
266-
if (x==parent[x])
279+
intparent=parents[x];
280+
281+
if (x==parent)
267282
returnx;
268283

269-
parent[x]=findRoot(parent[x]);// Error-prone point 2
284+
introot=findRoot(parent);// Error-prone point 2
270285
271-
returnparent[x];
286+
parents[x]=root;// Error-prone point 3
287+
288+
returnroot;
272289
}
273290

274-
boolsameRoot(intx,inty)
291+
boolisSameRoot(intx,inty)
275292
{
276293
returnfindRoot(x)==findRoot(y);
277294
}
@@ -280,16 +297,16 @@ public class Solution
280297

281298
##Go
282299
```go
283-
varparent []int
300+
varparents []int
284301

285302
funcfindRedundantConnection(edges [][]int) []int {
286-
parent =make([]int,len(edges) +1)
287-
fori:=0; i <len(parent); i++ {
288-
parent[i] = i
303+
parents =make([]int,len(edges) +1)
304+
fori:=0; i <len(parents); i++ {
305+
parents[i] = i
289306
}
290307

291308
for_,edge:=range edges {
292-
ifsameRoot(edge[0], edge[1]) {
309+
ifisSameRoot(edge[0], edge[1]) {
293310
return edge
294311
}
295312

@@ -303,32 +320,36 @@ func unite(x, y int) {
303320
rootX:=findRoot(x)
304321
rootY:=findRoot(y)
305322

306-
parent[rootY] = rootX// Error-prone point 1
323+
parents[rootY] = rootX// Error-prone point 1
307324
}
308325

309326
funcfindRoot(xint)int {
310-
if x == parent[x] {
327+
parent:= parents[x];
328+
329+
if x == parent {
311330
return x
312331
}
313332

314-
parent[x]=findRoot(parent[x])// Error-prone point 2
333+
root:=findRoot(parent)// Error-prone point 2
315334

316-
return parent[x]
335+
parents[x] = root// Error-prone point 3
336+
337+
return root
317338
}
318339

319-
funcsameRoot(x,yint)bool {
340+
funcisSameRoot(x,yint)bool {
320341
returnfindRoot(x) ==findRoot(y)
321342
}
322343
```
323344

324345
##Ruby
325346
```ruby
326347
deffind_redundant_connection(edges)
327-
@parent= []
328-
(0..edges.size).each { |i|@parent<< i }
348+
@parents= []
349+
(0..edges.size).each { |i|@parents<< i }
329350

330351
edges.eachdo |edge|
331-
ifsame_root(edge[0], edge[1])
352+
ifis_same_root(edge[0], edge[1])
332353
return edge
333354
end
334355

@@ -340,20 +361,24 @@ def unite(x, y)
340361
root_x= find_root(x)
341362
root_y= find_root(y)
342363

343-
@parent[root_y]= root_x# Error-prone point 1
364+
@parents[root_y]= root_x# Error-prone point 1
344365
end
345366

346367
deffind_root(x)
347-
if x==@parent[x]
368+
parent=@parents[x]
369+
370+
if x== parent
348371
return x
349372
end
350373

351-
@parent[x]= find_root(@parent[x])# Error-prone point 2
374+
root= find_root(parent)# Error-prone point 2
375+
376+
@parents[x]= root# Error-prone point 3
352377

353-
@parent[x]
378+
root
354379
end
355380

356-
defsame_root(x,y)
381+
defis_same_root(x,y)
357382
find_root(x)== find_root(y)
358383
end
359384
```

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp