[프로그래머스] 혼자 놀기의 달인

단간단간·2024년 3월 27일
0

알고리즘 문제

목록 보기
23/106

링크 : https://school.programmers.co.kr/learn/courses/30/lessons/131130
회고 : 연결 고리를 갖는 그룹은 어느 순서로 시작하든 동일한 그룹으로 묶인다.

python

def solution(cards):
    is_used_cards = [0 for _ in range(len(cards))]
    link_counts = []

    for i in range(len(cards)):
        if is_used_cards[i] == 0:
            link_cards = [i + 1]
            is_used_cards[i] = 1

            next = cards[i]
            while next not in link_cards:
                link_cards.append(next)
                is_used_cards[next - 1] = 1

                next = cards[next - 1]

            link_counts.append(len(link_cards))

    link_counts.sort(reverse=True)

    if len(link_counts) == 1:
        return 0
    else:
        return link_counts[0] * link_counts[1]
profile
simple is best

0개의 댓글