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

๐Ÿ“š Algorithm/Programmers

Programmers ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค - [๋‹ฌ๋ฆฌ๊ธฐ ๊ฒฝ์ฃผ] Python ํŒŒ์ด์ฌ ํ’€์ด

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

๐Ÿ“ ๋ฌธ์ œ

 

 

๐Ÿšซ ์ œํ•œ ์‚ฌํ•ญ

 

 

โœ๏ธ ์ž…์ถœ๋ ฅ

 

 

๐Ÿ’ก ํ’€์ด

์ฒ˜์Œ์—๋Š” res ๋ฆฌ์ŠคํŠธ ์„ ์–ธ๊ณผ callings์˜ ๋ฐ˜๋ณต๋ฌธ ๋•Œ๋ฌธ์— ์‹œ๊ฐ„์ดˆ๊ณผ ๊ฐ€ ๋‚˜์™”๋‹ค.

์ฝ”๋“œ๋ฅผ ์•„๋ฌด๋ฆฌ ๊ฐ„๊ฒฐํ•˜๊ฒŒ ์งœ๋„ ์‹œ๊ฐ„์ดˆ๊ณผ ๊ฐ€ ๋‚˜์˜ค๊ธธ๋ž˜ ๋‹ค๋ฅธ ์ •๋‹ต์ž ๋ถ„๋“ค์˜ ํ’€์ด๋ฅผ ์ฐธ๊ณ ํ•ด๋ณด๋‹ˆ ๊ฑฐ์˜ ๋Œ€๋ถ€๋ถ„์€ res๋ฅผ ๋”•์…”๋„ˆ๋ฆฌ๋กœ ์ ‘๊ทผ์„ ํ–ˆ๋‹ค. 

res๋ฅผ ๋”•์…”๋„ˆ๋ฆฌ๋กœ ๋ณ€๊ฒฝํ•œ ๊ฒƒ ์™ธ์—๋Š” ์ƒ๊ฐํ•œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ๋งž์•˜๋‹ค. python์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” swap์„ ํ•˜์˜€๋‹ค. 

 

def solution(players, callings):
    res = {player: i for i, player in enumerate(players)} 
    for i in callings:
        idx = res[i] 
        res[i] -= 1 
        res[players[idx-1]] += 1
        players[idx-1], players[idx] = players[idx], players[idx-1] 
    return players

 

 

 

 

728x90