1) ๋ฌธ์ ์ค๋ช
- ์์์๋ ๋งค๋ ๋ฌ๋ฆฌ๊ธฐ ๊ฒฝ์ฃผ๊ฐ ์ด๋ฆฝ๋๋ค.
- ํด์ค์ง๋ค์ ์ ์๋ค์ด ์๊ธฐ ๋ฐ๋ก ์์ ์ ์๋ฅผ ์ถ์ํ ๋ ์ถ์ํ ์ ์์ ์ด๋ฆ์ ๋ถ๋ฆ ๋๋ค.
- ์๋ฅผ ๋ค์ด 1๋ฑ๋ถํฐ 3๋ฑ๊น์ง "mumu", "soe", "poe" ์ ์๋ค์ด ์์๋๋ก ๋ฌ๋ฆฌ๊ณ ์์ ๋, ํด์ค์ง์ด "soe"์ ์๋ฅผ ๋ถ๋ ๋ค๋ฉด 2๋ฑ์ธ "soe" ์ ์๊ฐ 1๋ฑ์ธ "mumu" ์ ์๋ฅผ ์ถ์ํ๋ค๋ ๊ฒ์ ๋๋ค.
- ์ฆ "soe" ์ ์๊ฐ 1๋ฑ, "mumu" ์ ์๊ฐ 2๋ฑ์ผ๋ก ๋ฐ๋๋๋ค.
- ์ ์๋ค์ ์ด๋ฆ์ด 1๋ฑ๋ถํฐ ํ์ฌ ๋ฑ์ ์์๋๋ก ๋ด๊ธด ๋ฌธ์์ด ๋ฐฐ์ด players์ ํด์ค์ง์ด ๋ถ๋ฅธ ์ด๋ฆ์ ๋ด์ ๋ฌธ์์ด ๋ฐฐ์ด callings๊ฐ ๋งค๊ฐ๋ณ์๋ก ์ฃผ์ด์ง ๋, ๊ฒฝ์ฃผ๊ฐ ๋๋ฌ์ ๋ ์ ์๋ค์ ์ด๋ฆ์ 1๋ฑ๋ถํฐ ๋ฑ์ ์์๋๋ก ๋ฐฐ์ด์ ๋ด์ return ํ๋ solution ํจ์๋ฅผ ์์ฑํด์ฃผ์ธ์.
2) ์ ํ ์ฌํญ
- 5 ≤ players์ ๊ธธ์ด ≤ 50,000
- players[i]๋ i๋ฒ์งธ ์ ์์ ์ด๋ฆ์ ์๋ฏธํฉ๋๋ค.
- players์ ์์๋ค์ ์ํ๋ฒณ ์๋ฌธ์๋ก๋ง ์ด๋ฃจ์ด์ ธ ์์ต๋๋ค.
- players์๋ ์ค๋ณต๋ ๊ฐ์ด ๋ค์ด๊ฐ ์์ง ์์ต๋๋ค.
- 3 ≤ players[i]์ ๊ธธ์ด ≤ 10
- 2 ≤ callings์ ๊ธธ์ด ≤ 1,000,000
- callings๋ players์ ์์๋ค๋ก๋ง ์ด๋ฃจ์ด์ ธ ์์ต๋๋ค.
- ๊ฒฝ์ฃผ ์งํ์ค 1๋ฑ์ธ ์ ์์ ์ด๋ฆ์ ๋ถ๋ฆฌ์ง ์์ต๋๋ค.
3) ์ ์ถ๋ ฅ ์์
players callings result
["mumu", "soe", "poe", "kai", "mine"] ["kai", "kai", "mine", "mine"] ["mumu", "kai", "mine", "soe", "poe"]
4) ์ ์ถ๋ ฅ ์์ ์ค๋ช
์
์ถ๋ ฅ ์ #1
4๋ฑ์ธ "kai" ์ ์๊ฐ 2๋ฒ ์ถ์ํ์ฌ 2๋ฑ์ด ๋๊ณ ์์ 3๋ฑ, 2๋ฑ์ธ "poe", "soe" ์ ์๋ 4๋ฑ, 3๋ฑ์ด ๋ฉ๋๋ค.
5๋ฑ์ธ "mine" ์ ์๊ฐ 2๋ฒ ์ถ์ํ์ฌ 4๋ฑ, 3๋ฑ์ธ "poe", "soe" ์ ์๊ฐ 5๋ฑ, 4๋ฑ์ด ๋๊ณ ๊ฒฝ์ฃผ๊ฐ ๋๋ฉ๋๋ค.
1๋ฑ๋ถํฐ ๋ฐฐ์ด์ ๋ด์ผ๋ฉด ["mumu", "kai", "mine", "soe", "poe"]์ด ๋ฉ๋๋ค.
5) ํ์ด
def solution(players, callings):
# playerDict ์ ์ธ (key : ํ๋ ์ด์ด ์ด๋ฆ, value : ํ๋ ์ด์ด ์ธ๋ฑ์ค)
playerDict = dict()
# playerDict ์ด๊ธฐํ
for idx, player in enumerate(players):
playerDict[player] = idx
# callings ๋ฐฐ์ด ๋ฐ๋ณต
for player in callings:
# ์์ง๋ฅธ ์ ์์ ์ธ๋ฑ์ค
idx = playerDict.get(player)
# ๋ค์ณ์ง ์ ์์ ์ด๋ฆ
frontPlayer = players[idx - 1]
# ์ธ๋ฑ์ค ์์
playerDict[player] = idx - 1
playerDict[frontPlayer] = idx
players[idx - 1] = player
players[idx] = frontPlayer
return players
[Reference]
ํ๋ก๊ทธ๋๋จธ์ค
์ฝ๋ ์ค์ฌ์ ๊ฐ๋ฐ์ ์ฑ์ฉ. ์คํ ๊ธฐ๋ฐ์ ํฌ์ง์ ๋งค์นญ. ํ๋ก๊ทธ๋๋จธ์ค์ ๊ฐ๋ฐ์ ๋ง์ถคํ ํ๋กํ์ ๋ฑ๋กํ๊ณ , ๋์ ๊ธฐ์ ๊ถํฉ์ด ์ ๋ง๋ ๊ธฐ์ ๋ค์ ๋งค์นญ ๋ฐ์ผ์ธ์.
programmers.co.kr
๋ฐ์ํ
'๐ฏ Coding Test > ์๊ณ ๋ฆฌ์ฆ ํ ์คํธ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
ํ๋ก๊ทธ๋๋จธ์ค (Lv 2) - ์คํ์ฑํ ๋ฐฉ (0) | 2023.09.05 |
---|---|
ํ๋ก๊ทธ๋๋จธ์ค (Lv 2) - ์ฐ์ ๋ถ๋ถ ์์ด ํฉ์ ๊ฐ์ (1) | 2023.08.22 |
ํ๋ก๊ทธ๋๋จธ์ค (Lv 2) - ํ๋ ฌ์ ๊ณฑ์ (1) | 2023.08.21 |
ํ๋ก๊ทธ๋๋จธ์ค (Lv 2) - ๊ฒ์ ๋งต ์ต๋จ๊ฑฐ๋ฆฌ (1) | 2023.08.21 |
ํ๋ก๊ทธ๋๋จธ์ค (Lv 2) - ๊ตฌ๋ช ๋ณดํธ (1) | 2023.08.17 |