완주하지 못한 선수 - Python
2024. 1. 29. 22:48ㆍ공부/📝 프로그래머스
1. 풀이 코드
def solution(participant, completion):
list1 = sorted(participant)
list2 = sorted(completion)
for i, item in enumerate(list1):
if i < len(list2) and list1[i] != list2[i]:
return item
elif i == len(list2):
return item
return -1
위와 같이 풀었습니다.
2. 다른 사람 풀이 코드
def solution(participant, completion):
participant.sort()
completion.sort()
for i in range(len(completion)):
if participant[i] != completion[i]:
return participant[i]
return participant[len(participant)-1]
제 코드보다 간결하군요.
def solution(participant, completion):
answer = ''
temp = 0
dic = {}
for part in participant:
dic[hash(part)] = part
temp += int(hash(part))
for com in completion:
temp -= hash(com)
answer = dic[temp]
return answer
위와 같이 해시 풀이를 할 수 있겠습니다.
import collections
def solution(participant, completion):
answer = collections.Counter(participant) - collections.Counter(completion)
return list(answer.keys())[0]
역시나 파이썬답게 이런 것도 있군요.
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
'공부 > 📝 프로그래머스' 카테고리의 다른 글
옹알이 (2) - Python (0) | 2024.08.04 |
---|---|
둘만의 암호 - Python (0) | 2024.01.31 |
대충 만든 자판 - Python (0) | 2024.01.28 |
문자열 나누기 - Python (0) | 2024.01.27 |
숫자 짝꿍 - Python (0) | 2024.01.25 |