1. 문제 설명
2. 풀이 과정
collections 모듈에서 Counter 클래스를 임포트한다.
Counter는 데이터의 개수를 세는 클래스다.
participant의 개수와 completion의 개수를 Counter()로 participant_count와 completion_count에 각각 넣는다.
name,count를 participant_count.items()로 반복해서 받은 후
count가 completion_count.get(name,0)로 완주자 이름 중 해당 이름 카운트보다 participant_count가 클 때(동명이인)는 조건을 만족하고 completion_count와 participant_count가 같은 경우는 completion_count가 0이 되므로 조건을 만족한다.
- Counter는 dict의 하위 클래스로 dict과 비슷하게 키-값 쌍으로 데이터를 저장 (키는 요소, 값은 빈도를 의미)
- dict.get(key,default=None) : 찾고자 하는 키 key랑 키가 없을 때 default를 정의한다.
3. 코드
from collections import Counter
def solution(participant, completion):
participant_count = Counter(participant)
completion_count = Counter(completion)
for name, count in participant_count.items():
if count > completion_count.get(name, 0):
return name
4. 다른 사람의 풀이
import collections
def solution(participant, completion):
answer = collections.Counter(participant) - collections.Counter(completion)
return list(answer.keys())[0]
'프로그래머스 > 알고리즘 고득점 Kit' 카테고리의 다른 글
[프로그래머스] [스택/큐] Lv.1 /같은 숫자는 싫어 (파이썬/Python) (0) | 2025.03.29 |
---|---|
[프로그래머스] [해시] Lv.3 /베스트앨범 (파이썬/Python) (0) | 2025.03.28 |
[프로그래머스] [해시] Lv.2 /의상 (파이썬/Python) (0) | 2025.03.27 |
[프로그래머스] [해시] Lv.2 /전화번호 목록 (파이썬/Python) (0) | 2025.03.25 |
[프로그래머스] [해시] Lv.1 /폰켓몬 (파이썬/Python) (0) | 2025.03.19 |