์™„์ฃผํ•˜์ง€ ๋ชปํ•œ ์„ ์ˆ˜ - ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค(python3)

2022. 10. 3. 15:05ใ†Algorithm

์™„์ฃผํ•˜์ง€ ๋ชปํ•œ ์„ ์ˆ˜ - ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค(python3)

 

 

 ๋ฌธ์ œ ์„ค๋ช…

 

https://school.programmers.co.kr/learn/courses/30/lessons/42576?language=python3 

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”.

programmers.co.kr

 

 

 ํ•ด์‹œ๋ฅผ ์ด์šฉํ•œ ํ’€์ด

 

ํ•ด์‹œํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด์„œ ํ•ด์‹œ๊ฐ’์„ ๋„ฃ๊ณ  completion์„ ํƒ์ƒ‰ํ•˜๋ฉด์„œ ๋งจ ๋งˆ์ง€๋ง‰ ๊ฐ’์„ ์ถ”์ถœ

def solution(participant, completion):
    hashMap = {}
    
    sumHash = 0
    for part in participant:
        hashMap[hash(part)] = part
        sumHash += hash(part)
    
    for comp in completion:
        sumHash -= hash(comp)
    
    return hashMap[sumHash]

 

 

 

 

 ๋ฆฌ์ŠคํŠธ๋ฅผ ์ด์šฉํ•œ ํ’€์ด

 

์ •๋ ฌํ•˜๊ณ  ํƒ์ƒ‰ํ•˜๋Š” ๊ฒƒ์„ ์ƒ๊ฐํ•˜๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™๋‹ค

def solution(participant, completion):
    participant.sort()
    completion.sort()
    for part, com in zip(participant, completion):
        if part != com:
            return part
    
    return participant[-1]