프로그래머스/Level1
[해시 / 프로그래머스] 42576번 : 완주하지 못한 선수
ydin
2022. 8. 23. 11:27
- 문제 : https://school.programmers.co.kr/learn/courses/30/lessons/42576
Level1 문제고, 스스로 푼 문제다!
participant 중에 마라톤을 완료하지 못한 한 명을 찾아 이름을 반환해야한다
1.
not in completion으로 풀 수도 있지만, 동명이인인 경우에 participant에 동명이인이 있고, 그 중 한명이 completion에 없다면 complete하지 못했는데 complete했다고 여겨질 수 있다
2.
그래서 이름에 해당하는 사람 수를 딕셔너리에 저장한 후 completion에서 이름이 있을 때마다 값을 -= 1 하는 방식으로 진행한 다음
3.
통과하지 못한 사람은 마지막에 키 값이 1일 것이므로 해당 키를 반환하면 된다
- 정답 풀이 :
def solution(participant, completion):
result = {}
answer = ''
for name in participant :
if name not in result:
result[name] = 1
else:
result[name] += 1
for name in completion :
if name in result:
if result[name] != 0 :
result[name] -= 1
for name in result:
if result[name] != 0 :
answer += name
return answer
- 다른 풀이 :
Counter 모듈을 이용해서 각 리스트의 차이를 구해 정답을 찾는 풀이다
from collections import Counter
def solution(participant, completion):
result = Counter(participant) - Counter(completion)
return list(result.keys())[0]