๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐Ÿ Python/solve-algorithm

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ํ•ด์‹œ [์™„์ฃผํ•˜์ง€ ๋ชปํ•œ ์„ ์ˆ˜] : ํŒŒ์ด์ฌ

์ด์ œ ์Šฌ์Šฌ ๋ฐฑ์ค€ ํƒˆ์ถœํ•˜๊ณ  ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค๋ฅผ ๊ฒธํ•  ๋•Œ๊ฐ€ ์™”๋‹ค.

์›๋ž˜๋Š” ๋ฐฑ์ค€ ๊ณจ๋“œ5 ์ฐ์œผ๋ฉด ๋„˜์–ด๊ฐˆ ๊ณ„ํš์ด์—ˆ์œผ๋‚˜

1. ์š”์ฆ˜ 1์ผ1์ปค๋ฐ‹๋„ ์˜ˆ์ „๋งŒํผ ๋ชปํ•˜๊ณ  ์žˆ์–ด์„œ ์ง„๋„๊ฐ€ ์˜ ์•ˆ ๋‚˜๊ณ 

2. ๋‚ด๊ฐ€ ์ง€์›ํ–ˆ๋˜ ๊ธฐ์—…์€ ์ฝ”ํ…Œ๋ฅผ ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค๋ฅผ ํ™œ์šฉํ•˜์—ฌ ์‘์‹œํ•  ์ˆ˜ ์žˆ์–ด์„œ ์ ์‘ํ•˜๊ณ ์ž

๋ณ‘๋ ฌ์ ์œผ๋กœ ์ง„ํ–‰ํ•˜๊ธฐ๋กœ ํ–ˆ๋‹ค.

(์‚ฌ์‹ค ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ํฌ๋น„์•„(?)๊ฐ€ ์žˆ์—ˆ๋‹ค.. ๊ทน๋ณตํ•ด๋ณด์ž๊ณ !)


๐Ÿ“Ž Problem

https://school.programmers.co.kr/learn/courses/30/lessons/42576

 

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

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

programmers.co.kr

 


๐Ÿ“Ž Solution

์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ๋ฅผ ํ’€ ๋•Œ, ํ•ญ์ƒ ์ผ๋‹จ์€ ์ด์ƒํ•˜๊ฒŒ๋ผ๋„ ํ’€์–ด๋ณธ๋‹ค.

๊ทธ๋ฆฌ๊ณ  ๋‹ต์ด ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ๋‚˜์˜ค๋ฉด, ๊ณต๊ฐ„ ์‹œ๊ฐ„ ๋ณต์žก๋„ ๋“ฑ์„ ๊ณ ๋ คํ•˜์—ฌ ๋‹ค๋ฅธ ํ’€์ด๋„ ์ ์šฉํ•ด๋ณด๊ณ  ์ฝ”๋“œ๋„ ์งง๊ฒŒ ํ•ด๋ณด๋ ค๊ณ  ํ•˜๋Š” ํŽธ์ด๋‹ค.

์ฒซ๋ฒˆ์งธ ํ’€์ด๋Š” ์ •ํ™•์„ฑ์€ ๋งž์•˜๋Š”๋ฐ ํšจ์šฉ์„ฑ ํ…Œ์ŠคํŠธ์—์„œ 0์ ์œผ๋กœ ๋ถˆํ•ฉ๊ฒฉํ–ˆ๋‹ค^^

 

1๏ธโƒฃ ์ฒซ๋ฒˆ์งธ ๋ฐฉ๋ฒ•

๊ทธ๋ž˜์„œ ๋‹ค์‹œ ์‹œ๋„ํ•œ ๋ฐฉ๋ฒ•์€ ๋‘ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ •๋ ฌํ•˜์—ฌ ํ•˜๋‚˜์”ฉ ๋น„๊ตํ•˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ ํ’€์ดํ•ด๋ณด์•˜๋‹ค.

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์—์„œ๋„ 3๋ฒˆ์งธ๋กœ ์ธ๊ธฐ์žˆ๋Š” ๋ฐฉ๋ฒ•์ด์—ˆ๋‹ค.

 

# case 1
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]

 

2๏ธโƒฃ ๋‘๋ฒˆ์งธ ๋ฐฉ๋ฒ•

๋ฌธ์ œ ๋ถ„๋ฅ˜๊ฐ€ ํ•ด์‹œ๋กœ ๋˜์–ด์žˆ๋Š” ๋งŒํผ ํ•ด์‹œ๋กœ ํ’€์ดํ•ด ๋ณด์ž!

 

# case 2 : ํ•ด์‹œ
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

# test code
print(solution(["leo", "kiki", "eden"], ["eden", "kiki"]))

 

3๏ธโƒฃ ์„ธ๋ฒˆ์งธ ๋ฐฉ๋ฒ•

๋งˆ์ง€๋ง‰์œผ๋กœ ๋‹ค๋ฅธ ์‚ฌ๋žŒ ํ’€์ด์—์„œ ๊ฐ€์žฅ ์ธ๊ธฐ๊ฐ€ ์žˆ์—ˆ๋˜ ๋ฐฉ๋ฒ•!

collections์ด๋ผ๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์•Œ๊ณ ๋Š” ์žˆ์—ˆ๋Š”๋ฐ, ์ด์ •๋„๋กœ ๊ฐ„๋‹จํ•˜๊ฒŒ ํ’€์ดํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค๋‹ˆ.. ์• ์šฉํ•ด์•ผ๊ฒ ๋‹ค!

 

# case 3 : collections ํ™œ์šฉ(๊ฐ€์žฅ ๊ฐ„๋‹จ!)
import collections

def solution(participant, completion):
    answer = collections.Counter(participant) - collections.Counter(completion)
    return list(answer.keys())[0]