Algorithm/1일 1코테

[😍 프로그래머스] 완주하지 못한 선수 - hash

대인보우 2021. 7. 22. 20:07
반응형

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

 

코딩테스트 연습 - 완주하지 못한 선수

수많은 마라톤 선수들이 마라톤에 참여하였습니다. 단 한 명의 선수를 제외하고는 모든 선수가 마라톤을 완주하였습니다. 마라톤에 참여한 선수들의 이름이 담긴 배열 participant와 완주한 선수

programmers.co.kr

 

✅ 내가 푼 답

def solution(participant, completion):
    hash_dict = {}
    
    # 참여자들 count, 이름이 같은 사람은 2가 된다. 
    for person in participant:
        if person not in hash_dict:
            hash_dict[person] = 1
        else:
            hash_dict[person] += 1
    
    # 완주자들 같은 이름 -1
    for person in completion:
        if person in hash_dict:
            hash_dict[person] -= 1
    
    # 0이 아니면 리턴
    for i in hash_dict.items():
        if i[1] != 0:
            return i[0]

✅ 베스트 답안

import collections

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

카운터-카운터가 통하다니....!!!!!!!!!!

 

반응형