[프로그래머스] 튜플 (Python)

cotato·2021년 5월 7일
0

https://programmers.co.kr/learn/courses/30/lessons/64065

My Solution

import heapq

def solution(s):
    s = s[2:-2]
    s = s.split('},{')
    
    heap = []
    for elem in s:
        elem_set = set(elem.split(','))
        heapq.heappush(heap, (len(elem_set), elem_set))
    
    answer = [int(min(heap[0][1]))]
    while len(heap) >= 2:
        popped = heapq.heappop(heap)
        answer.append(int(min(heap[0][1] - popped[1])))
    
    return answer

풀고 나서 알았지만, 전체 문자열에서 숫자가 등장하는 횟수가 1개씩 차이나기 때문에, Counter를 사용하고 등장 횟수대로 정렬하면 훨씬 더 적은 코드로 풀 수 있다.

profile
coding_potato

0개의 댓글