-
[프로그래머스 42576번] 완주하지 못한 선수Coding Test/프로그래머스 2020. 11. 17. 18:36
# 문제 설명
수많은 마라톤 선수들이 마라톤에 참여하였습니다. 단 한 명의 선수를 제외하고는 모든 선수가 마라톤을 완주하였습니다.
마라톤에 참여한 선수들의 이름이 담긴 배열 participant와 완주한 선수들의 이름이 담긴 배열 completion이 주어질 때, 완주하지 못한 선수의 이름을 return 하도록 solution 함수를 작성해주세요.
# 제한사항
· 마라톤 경기에 참여한 선수의 수는 1명 이상 100,000명 이하입니다.
· completion의 길이는 participant의 길이보다 1 작습니다.
· 참가자의 이름은 1개 이상 20개 이하의 알파벳 소문자로 이루어져 있습니다.
· 참가자 중에는 동명이인이 있을 수 있습니다.
# 입출력 예
participant completion return
[leo, kiki, eden] [eden, kiki] leo [marina, josipa, nikola, vinko, filipa] [josipa, filipa, marina, nikola] vinko [mislav, stanko, mislav, ana] [stanko, ana, mislav] mislav # 입출력 예 설명
예제 #1
leo는 참여자 명단에는 있지만, 완주자 명단에는 없기 때문에 완주하지 못했습니다.
예제 #2
vinko는 참여자 명단에는 있지만, 완주자 명단에는 없기 때문에 완주하지 못했습니다.
예제 #3
mislav는 참여자 명단에는 두 명이 있지만, 완주자 명단에는 한 명밖에 없기 때문에 한명은 완주하지 못했습니다.
내 풀이
def solution(participant, completion): answer = '' # 두 배열을 오름차순으로 정렬 participant.sort() completion.sort() # 완주한 선수들의 숫자만큼 비교 for i in range(len(completion)): # 두 배열의 동일한 인덱스에 해당하는 값이 서로 다르다면 if participant[i] != completion[i]: answer = participant[i] # 해당 인덱스의 participant 값이 완주하지 못한 선수 이름 return answer # answer가 빈 문자열이라면 if not answer: # participant 배열의 마지막 값이 완주하지 못한 선수 이름 answer = participant[len(participant) - 1] return answer
'Coding Test > 프로그래머스' 카테고리의 다른 글
[프로그래머스 49993번] 스킬트리 (0) 2020.11.17 [프로그래머스 42586번] 기능개발 (0) 2020.11.17 [프로그래머스 68644번] 두 개 뽑아서 더하기 (0) 2020.11.17 [프로그래머스 64061번] 크레인 인형뽑기 게임 (0) 2020.11.03 [프로그래머스 42748번] K번째수 (0) 2020.11.03