본문 바로가기
카테고리 없음

백준 1715

by SayHiWorld 2024. 10. 4.

 

=> 카드 묶음에서 가장 작은 값, 두번째로 가장 작은 값을 더한다.
 

 

합한 뒤에, 다시 이 값을 리스트에 포함해 가장 작은 값, 두번째로 가장 작은 값을 더한다. 

이것을 반복한다. 

def min_comparison(card_bundles):
    # 총 비교 횟수 저장 변수
    total_comparisons = 0
    
    # 카드 묶음이 하나만 남을 때까지 반복
    while len(card_bundles) > 1:
        # 리스트를 정렬하여 가장 작은 두 묶음을 선택
        card_bundles.sort()
        first = card_bundles.pop(0)
        second = card_bundles.pop(0)
        
        # 두 묶음을 합쳐서 비교 횟수에 추가
        combined = first + second
        total_comparisons += combined
        
        # 합쳐진 묶음을 다시 리스트에 추가
        card_bundles.append(combined)
    
    return total_comparisons