algorithm_programmers : 완주하지 못한 선수

박민우·2022년 1월 27일
0

algorithm

목록 보기
1/3


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

collections.Counter(participant) - collction.Counter(completion)
Counter class는 상호간의 뺄셈 연산을 지원한다.
즉, 뺄셈 연산 한번으로 둘 participant는 있고, completion에는 없는 사람을 찾을 수 있다.

출처: https://coding-grandpa.tistory.com/85

def Solution(participant, completion):
    answer = []
    participant.sort()
    completion.sort()
    for i in range(len(completion)):
        if(participant[i] != completion[i]):
            return participant[i]

Counter class가 생각나지 않을 때에는 정렬해서 구하는 간단한 방법이 있다!

profile
KingdomOfGod newPerson = new KingdomOfGod();

0개의 댓글