๋‚ด ์ธ์ƒ์—์„œ ๋ฏฟ์„ ๊ฑด ์˜ค์ง ๋‚˜ ์ž์‹ ๋ฟ!

The only one you can truly trust is yourself.

๊ฒŒ์ž„ ํ”„๋กœ๊ทธ๋ž˜๋ฐ/Python ํ”„๋กœ๊ทธ๋ž˜๋ฐ

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค LV1 ๋‹ฌ๋ฆฌ๊ธฐ ๊ฒฝ์ฃผ

๐ŸŽฎinspirer9 2023. 6. 23. 00:05
728x90
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ ์„ค๋ช…

์–€์—์„œ๋Š” ๋งค๋…„ ๋‹ฌ๋ฆฌ๊ธฐ ๊ฒฝ์ฃผ๊ฐ€ ์—ด๋ฆฝ๋‹ˆ๋‹ค. ํ•ด์„ค์ง„๋“ค์€ ์„ ์ˆ˜๋“ค์ด ์ž๊ธฐ ๋ฐ”๋กœ ์•ž์˜ ์„ ์ˆ˜๋ฅผ ์ถ”์›”ํ•  ๋•Œ ์ถ”์›”ํ•œ ์„ ์ˆ˜์˜ ์ด๋ฆ„์„ ๋ถ€๋ฆ…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด 1๋“ฑ๋ถ€ํ„ฐ 3๋“ฑ๊นŒ์ง€ "mumu", "soe", "poe" ์„ ์ˆ˜๋“ค์ด ์ˆœ์„œ๋Œ€๋กœ ๋‹ฌ๋ฆฌ๊ณ  ์žˆ์„ ๋•Œ, ํ•ด์„ค์ง„์ด "soe"์„ ์ˆ˜๋ฅผ ๋ถˆ๋ €๋‹ค๋ฉด 2๋“ฑ์ธ "soe" ์„ ์ˆ˜๊ฐ€ 1๋“ฑ์ธ "mumu" ์„ ์ˆ˜๋ฅผ ์ถ”์›”ํ–ˆ๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ฆ‰ "soe" ์„ ์ˆ˜๊ฐ€ 1๋“ฑ, "mumu" ์„ ์ˆ˜๊ฐ€ 2๋“ฑ์œผ๋กœ ๋ฐ”๋€๋‹ˆ๋‹ค.

์„ ์ˆ˜๋“ค์˜ ์ด๋ฆ„์ด 1๋“ฑ๋ถ€ํ„ฐ ํ˜„์žฌ ๋“ฑ์ˆ˜ ์ˆœ์„œ๋Œ€๋กœ ๋‹ด๊ธด ๋ฌธ์ž์—ด ๋ฐฐ์—ด players์™€ ํ•ด์„ค์ง„์ด ๋ถ€๋ฅธ ์ด๋ฆ„์„ ๋‹ด์€ ๋ฌธ์ž์—ด ๋ฐฐ์—ด callings๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์งˆ ๋•Œ, ๊ฒฝ์ฃผ๊ฐ€ ๋๋‚ฌ์„ ๋•Œ ์„ ์ˆ˜๋“ค์˜ ์ด๋ฆ„์„ 1๋“ฑ๋ถ€ํ„ฐ ๋“ฑ์ˆ˜ ์ˆœ์„œ๋Œ€๋กœ ๋ฐฐ์—ด์— ๋‹ด์•„ return ํ•˜๋Š” solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.

์ œํ•œ ์‚ฌํ•ญ

  • 5 ≤ players์˜ ๊ธธ์ด ≤ 50,000
    • players[i]๋Š” i๋ฒˆ์งธ ์„ ์ˆ˜์˜ ์ด๋ฆ„์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.
    • players์˜ ์›์†Œ๋“ค์€ ์•ŒํŒŒ๋ฒณ ์†Œ๋ฌธ์ž๋กœ๋งŒ ์ด๋ฃจ์–ด์ ธ ์žˆ์Šต๋‹ˆ๋‹ค.
    • players์—๋Š” ์ค‘๋ณต๋œ ๊ฐ’์ด ๋“ค์–ด๊ฐ€ ์žˆ์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
    • 3 ≤ players[i]์˜ ๊ธธ์ด ≤ 10
  • 2 ≤ callings์˜ ๊ธธ์ด ≤ 1,000,000
    • callings๋Š” players์˜ ์›์†Œ๋“ค๋กœ๋งŒ ์ด๋ฃจ์–ด์ ธ ์žˆ์Šต๋‹ˆ๋‹ค.
    • ๊ฒฝ์ฃผ ์ง„ํ–‰์ค‘ 1๋“ฑ์ธ ์„ ์ˆ˜์˜ ์ด๋ฆ„์€ ๋ถˆ๋ฆฌ์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ

players  callings  result
["mumu", "soe", "poe", "kai", "mine"] ["kai", "kai", "mine", "mine"] ["mumu", "kai", "mine", "soe", "poe"]

์ž…์ถœ๋ ฅ ์˜ˆ ์„ค๋ช…

  • ์ž…์ถœ๋ ฅ ์˜ˆ #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"]์ด ๋ฉ๋‹ˆ๋‹ค.

ํ’€์ด

  • ์ด๋ฆ„์ด ๋ถˆ๋ฆฐ ์• ๋ž‘ ๊ทธ ์•ž์— ์žˆ๋Š” ์• ๋ž‘ ์Šค์™‘ํ•ด์ฃผ๋ฉด ๋˜๋Š”๊ฑฐ ์•„๋‹Œ๊ฐ€?
  • ...๋Š” ์‹œ๊ฐ„์ดˆ๊ณผ์— ๊ฑธ๋ฆผ.

  • swap์ด ์‹œ๊ฐ„์ดˆ๊ณผ์— ๊ฑธ๋ ธ์„๋ฆฌ๋Š” ์—†๊ณ , for๋ฌธ์œผ๋กœ ๋Œ๋ฆฌ๋ฉด์„œ index๋กœ ์œ„์น˜ ์ฐพ๋Š” ๋ถ€๋ถ„์ด ๋ฌธ์ œ ๊ฐ™์Œ.
  • ๊ทธ๋ž˜์„œ ํ•ด์‰ฌ๋กœ ๋˜์–ด ์žˆ๋Š” ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ 2๊ฐœ ๋งŒ๋“ค์–ด์„œ, key๋กœ value ์ฐพ๋Š” ์•  ํ•˜๋‚˜, value๋กœ ํ‚ค ์ฐพ๋Š” ์•  ํ•˜๋‚˜ ๋งŒ๋“ค์–ด์„œ ์ด์ค‘ ์Šค์™‘ํ•ด์คฌ๋‹ค.
  • ๊ทธ๋ฆฌ๊ณ  ๊ฒฐ๊ณผ๋กœ ๋‚˜์˜จ ๊ฑธ value ๊ธฐ์ค€์œผ๋กœ ๋‹ค์‹œ ์ •๋ ฌํ•˜๊ณ , key๋งŒ ๋ฝ‘์•„์„œ ๋ฆฌ์ŠคํŠธ๋กœ ๋งŒ๋“ค์—ˆ๋‹ค.
def solution(players, callings):
    dic = {}
    reverse_dic = {}
    answer = []
    
    for i,p in enumerate(players):
        dic[p] = i
        reverse_dic[i] = p
    
    for c in callings:
        i = dic[c]
        cc = reverse_dic[i-1]
        dic[c],dic[cc] = dic[cc],dic[c]
        reverse_dic[i],reverse_dic[i-1] = reverse_dic[i-1],reverse_dic[i]
        
    dic2 = sorted(dic.items(), key=lambda x:x[1])
    for d in dic2:
        answer.append(d[0])
        
    return answer
  • ์ข€ ๊พธ์งˆ๊พธ์งˆํ•˜๊ธด ํ•˜์ง€๋งŒ ํ’€์—ˆ๋‹ค... ใ…‹ใ…‹

728x90
๋ฐ˜์‘ํ˜•