leetcode.com/problems/friend-circles/
Union Find 문제
유니온 파인드 개념: kakunblog.tistory.com/16
class Solution(object):
def findCircleNum(self, M):
sets = [i for i in range(len(M))]
def find(x):
if x == sets[x]:
return x
sets[x] = find(sets[x])
return sets[x]
for j in range(0, len(M)):
for i in range(j+1, len(M)):
if M[i][j] == 1:
sets[find(i)] = find(j)
return sum([1 for i, v in enumerate(sets) if i == v])
다른 유니온 파인드 문제: kakunblog.tistory.com/19
'릿코드Leetcode' 카테고리의 다른 글
Union Find [파이썬] 684. Redundant Connection 릿코드 leetcode [1] (0) | 2020.11.09 |
---|---|
[파이썬] 1299. Replace Elements with Greatest Element on Right Side (0) | 2020.11.06 |