Notice Period

The Extra Cable to Cut

medium

A network team meant to wire n switches (numbered 1 to n) as a tree — n - 1 cables, no loops. Someone patched in one extra cable, creating exactly one loop.

Given the n cables as edges (each [u, v], undirected, in the order they were installed), return the cable that should be unplugged to restore a tree. If several candidates would work, return the one installed last (latest in the input).

Example 1
in edges = [[1,2],[1,3],[2,3]]
out [2,3]
Switches 1, 2, 3 form a triangle; the last cable closing it is [2,3].
Example 2
in edges = [[1,2],[2,3],[3,4],[1,4],[1,5]]
out [1,4]
[1,4] closes the loop 1-2-3-4; [1,5] is a harmless branch added later.
Constraints
  • · 3 <= n <= 100
  • · edges has exactly n entries
  • · the graph is connected with exactly one cycle
similar problem on LeetCode ↗
solution.pyloading python…
test results
Hit ▶ Run to test your code against the visible cases.