快轉到主要內容
  1. LeetCode/

LeetCode 684: Redundant Connection

·1 分鐘· ·
LeetCode Daily Medium
Wei Yi Chung
作者
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
目錄

基本資料
#

難易度: medium 第一次嘗試: 2025-05-04

  • 總花費時間:04:00.00

解題思路
#

這題要找出最後一條會導致圖中出現環(cycle)的邊。
根據並查集(Union Find)演算法,當我們對兩個節點進行 union 操作時,如果它們已經有相同的根(parent),就代表這條邊會形成環。
因此,我們只需要記錄這條邊並回傳即可。

解法
#

class Solution:
    def findRedundantConnection(self, edges: List[List[int]]) -> List[int]:
        n = len(edges)
        ranking = [0 for _ in range(n)]
        parent = [i for i in range(n)]
        res = []

        def find(x):
            if parent[x] != x:
                parent[x] = find(parent[x])
            return parent[x]

        def union(x, y):
            nonlocal res
            x_root = find(x)
            y_root = find(y)

            if x_root == y_root:
                res = [x + 1, y + 1]
                return

            if ranking[x_root] > ranking[y_root]:
                parent[y_root] = parent[x_root]
                ranking[x_root] += ranking[y_root]
            else:
                parent[x_root] = parent[y_root]
                ranking[y_root] += ranking[x_root]

        for x, y in edges:
            union(x - 1, y - 1)

        return res

收穫
#

遇到的問題
#

相關文章

LeetCode 2799: Count Complete Subarrays in an Array
·1 分鐘
LeetCode Daily Medium
LeetCode 解題紀錄
LeetCode 2145: Count the Hidden Sequences
·1 分鐘
LeetCode Daily Medium
LeetCode 解題紀錄
LeetCode 781: Rabbits in Forest
·1 分鐘
LeetCode Daily Medium
LeetCode 解題紀錄
LeetCode 560: Subarray Sum Equals K
·1 分鐘
LeetCode Daily Medium
LeetCode 解題紀錄
LeetCode 1922: Count Good Numbers
·1 分鐘
LeetCode Daily Medium
LeetCode 解題紀錄
LeetCode 547: Number of Provinces
·2 分鐘
LeetCode Union Find Medium
LeetCode 解題紀錄