Skip to content

Commit f70dee4

Browse files
authored
Update redundant-connection.py
1 parent f040213 commit f70dee4

1 file changed

Lines changed: 28 additions & 0 deletions

File tree

Python/redundant-connection.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,34 @@
11
# Time: O(nlog*n) ~= O(n), n is the length of the positions
22
# Space: O(n)
33

4+
# We are given a "tree" in the form of a 2D-array, with distinct values for each node.
5+
#
6+
# In the given 2D-array, each element pair [u, v] represents that v is a child of u in the tree.
7+
#
8+
# We can remove exactly one redundant pair in this "tree" to make the result a (rooted) tree.
9+
#
10+
# You need to find and output such a pair. If there are multiple answers for this question,
11+
# output the one appearing last in the 2D-array. There is always at least one answer.
12+
#
13+
# Example 1:
14+
# Input: [[1,2], [1,3], [2,3]]
15+
# Output: [2,3]
16+
# Explanation: Original tree will be like this:
17+
# 1
18+
# / \
19+
# 2 - 3
20+
#
21+
# Example 2:
22+
# Input: [[1,2], [1,3], [3,1]]
23+
# Output: [3,1]
24+
# Explanation: Original tree will be like this:
25+
# 1
26+
# / \\
27+
# 2 3
28+
# Note:
29+
# The size of the input 2D-array will be between 1 and 1000.
30+
# Every integer represented in the 2D-array will be between 1 and 2000.
31+
432
class UnionFind(object):
533
def __init__(self, n):
634
self.set = range(n)

0 commit comments

Comments
 (0)