110. The Cable That Closes the Loop
Medium
A network with n points was connected and had no cycles. Then one additional cable was installed, creating exactly one cycle. Points are numbered from 1 to n, and cabos[i] represents the cable installed at position i.
Return the last cable installed that belongs to the cycle, as a pair of numbers. Removing it must leave the network connected and cycle-free. Preserve the order of the two points as they appear in cabos.
Example 1
Input: cabos = [[1,2],[1,3],[2,3]]
Output: [2,3]
Explanation: The cable [2,3] closes the cycle formed by the three points.
Example 2
Input: cabos = [[1,2],[2,3],[3,4],[1,4],[1,5]]
Output: [1,4]
Explanation: The cable [1,4] is the last installed cable that belongs to the cycle.
Constraints
- 3 <= cabos.length <= 10^5
- cabos.length == n, where n is the number of points.
- cabos[i].length == 2
- 1 <= cabos[i][j] <= n
- cabos[i][0] != cabos[i][1]
- The network consists of a tree with one additional cable, with no duplicate cables.
GraphsUnion-find
Saved in this browser
Caso 1
cabos =
[[1,2],[1,3],[2,3]]
Expected =
[2,3]
Caso 2
cabos =
[[1,2],[2,3],[3,4],[1,4],[1,5]]
Expected =
[1,4]
Run checks these 2 cases. Submit runs all 11, including hidden ones.