Skip to content

Commit 0f7eea1

Browse files
committed
Sync LeetCode submission Runtime - 11 ms (17.39%), Memory - 18.2 MB (51.11%)
1 parent 85d2354 commit 0f7eea1

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<p>In this problem, a tree is an <strong>undirected graph</strong> that is connected and has no cycles.</p>
2+
3+
<p>You are given a graph that started as a tree with <code>n</code> nodes labeled from <code>1</code> to <code>n</code>, with one additional edge added. The added edge has two <strong>different</strong> vertices chosen from <code>1</code> to <code>n</code>, and was not an edge that already existed. The graph is represented as an array <code>edges</code> of length <code>n</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> indicates that there is an edge between nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code> in the graph.</p>
4+
5+
<p>Return <em>an edge that can be removed so that the resulting graph is a tree of </em><code>n</code><em> nodes</em>. If there are multiple answers, return the answer that occurs last in the input.</p>
6+
7+
<p>&nbsp;</p>
8+
<p><strong class="example">Example 1:</strong></p>
9+
<img alt="" src="https://assets.leetcode.com/uploads/2021/05/02/reduntant1-1-graph.jpg" style="width: 222px; height: 222px;" />
10+
<pre>
11+
<strong>Input:</strong> edges = [[1,2],[1,3],[2,3]]
12+
<strong>Output:</strong> [2,3]
13+
</pre>
14+
15+
<p><strong class="example">Example 2:</strong></p>
16+
<img alt="" src="https://assets.leetcode.com/uploads/2021/05/02/reduntant1-2-graph.jpg" style="width: 382px; height: 222px;" />
17+
<pre>
18+
<strong>Input:</strong> edges = [[1,2],[2,3],[3,4],[1,4],[1,5]]
19+
<strong>Output:</strong> [1,4]
20+
</pre>
21+
22+
<p>&nbsp;</p>
23+
<p><strong>Constraints:</strong></p>
24+
25+
<ul>
26+
<li><code>n == edges.length</code></li>
27+
<li><code>3 &lt;= n &lt;= 1000</code></li>
28+
<li><code>edges[i].length == 2</code></li>
29+
<li><code>1 &lt;= a<sub>i</sub> &lt; b<sub>i</sub> &lt;= edges.length</code></li>
30+
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
31+
<li>There are no repeated edges.</li>
32+
<li>The given graph is connected.</li>
33+
</ul>
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Approach 1: Depth-First Search - Brute Force
2+
3+
# Time: O(n ^ 2)
4+
# Space: O(n)
5+
6+
class Solution:
7+
def _is_connected(self, src, target, visited, adj_list):
8+
visited[src] = True
9+
10+
if src == target:
11+
return True
12+
13+
is_found = False
14+
for adj in adj_list[src]:
15+
if not visited[adj]:
16+
is_found = is_found or self._is_connected(adj, target, visited, adj_list)
17+
18+
return is_found
19+
20+
21+
def findRedundantConnection(self, edges: List[List[int]]) -> List[int]:
22+
n = len(edges)
23+
24+
adj_list = [[] for _ in range(n)]
25+
26+
for edge in edges:
27+
visited = [False] * n
28+
29+
if self._is_connected(edge[0] - 1, edge[1] - 1, visited, adj_list):
30+
return edge
31+
32+
adj_list[edge[0] - 1].append(edge[1] - 1)
33+
adj_list[edge[1] - 1].append(edge[0] - 1)
34+
35+
return []

0 commit comments

Comments
 (0)