programmers- lv.1 (완주하지 못한 선수)

이예송·2023년 7월 21일

PS

목록 보기
55/97

문제링크: 완주하지 못한 선수

✍🏻 Information

content
언어python
난이도⭐️
풀이시간5분
제출횟수2
인터넷검색유무no




🍒 My Code

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




💡 What I learned

  • 나는 sorting을 사용하였는데 아래와 같이 hash를 이용하는 방법도 있다.
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
  • dictionary를 사용함으로써 hash의 단점인 공간복잡도가 높아질수있음을 막아준것같다. 내 풀이는 시간복잡도가 nlogn인데 hash를 사용하면 n이다.
  • hash() : key-value 중 키를 생성해주는 함수. 실행할 때마다 값이 달라질 수 있음.

0개의 댓글