본문 바로가기

릿코드Leetcode

Union Find [파이썬] 684. Redundant Connection 릿코드 leetcode [1]

leetcode.com/problems/redundant-connection/

 

 

Union Find를 이용해서 풀기

유니온 파인드 개념: kakunblog.tistory.com/16

 

class Solution(object):
    def findRedundantConnection(self, edges):
        sets = [0] * (len(edges)+1)

        def find(x):
            if sets[x] == 0:
                return x
            return find(sets[x])

        for x, y in edges:
            u = find(x)
            v = find(y)
            if u == v:
                return [x, y]
            sets[u] = v

 

다른 유니온 파인드 문제: kakunblog.tistory.com/20