L2 : 섬 연결하기 Python

jhyunn·2023년 1월 20일
0

Programmers

목록 보기
48/69

L2 : 섬 연결하기 Python

https://school.programmers.co.kr/learn/courses/30/lessons/42861

def solution(n, costs):
    answer = 0
    costs.sort(key = lambda x: x[2]) # 비용 기준 sort
    link = set([costs[0][0]]) # set으로 해야함

    while len(link) != n:
        for v in costs:
            if v[0] in link and v[1] in link: #
                continue
            if v[0] in link or v[1] in link:
                link.update([v[0], v[1]]) # update!!
                answer += v[2]
                break

    return answer

#Kruskal #크루스칼 #그리디 #Greedy

profile
https://github.com/Sungjeonghyun

0개의 댓글