코딩테스트 | python/프로그래머스

[프로그래머스 | python] 완주하지 못한 선수

iemxl 2024. 2. 6. 19:07

def solution(participant, completion):
    answer = ''
    for i in completion:
        if i in participant:
            participant.remove(i)
    answer = participant[0]
    return answer

 

>>시간초과

>>in을 사용하면서 또 for문이 사용되어 시간초과가 나는것 같다

 

 

def solution(participant, completion):
    answer = ''
    participant.sort()
    completion.sort()
    for i in range(len(completion)):
        if participant[i] != completion[i]:
            answer+=participant[i]
            break
    if answer == '':
        answer+=participant[-1]
    return answer

 

#해시를 사용한 방식

def solution(participant, completion):
    sum = 0
    dic = {}
    for x in participant:
        dic[hash(x)] = x
        sum += hash(x)
    for x in completion:
        sum -= hash(x)
    answer = dic[sum]
    return answer