[Python/Programmers] 완주하지 못한 선수

정나린·2022년 3월 1일

1. 난이도: 프로그래머스 LEVEL 1

2. 문제요약: 완주하지 못한 한 명의 이름을 반환하라

3. 문제핵심: hash(map)을 사용해서 문제를 풀 수 있는가

Counter 함수

1) collections 모듈의 함수로, list 상에서 요소들과 요소들의 개수를 dictionary 형태로 반환한다.

participant = ['tom', 'peter', 'spiderman', 'tom']
Counter(participant) => {'tom': 2, 'peter':1, 'spiderman':1}

name = 'apple'
Conter(name) => {'a':1, 'e':1, 'l':1, 'p':2}

2) 시간복잡도

Counter는 dict의 하위 클래스로 생성시 O(n)의 시간 복잡도를 가진다.
(출처: https://nero.devstory.co.kr/post/pl-python-collections-counter/)

3) 코드

from collections import Counter

def solution(participant, completion):
    answer = list((Counter(participant) - Counter(completion)).keys())[0]
    return answer

0개의 댓글