Potato
์•ˆ๋…•ํ•˜์„ธ์š”, ๊ฐ์žก๋‹ˆ๋‹ค?๐Ÿฅ” ^___^ ๐Ÿ˜บ github ๋ฐ”๋กœ๊ฐ€๊ธฐ ๐Ÿ‘‰๐Ÿป

Algorithm/Programmers

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ํ‚คํŒจ๋“œ ๋ˆ„๋ฅด๊ธฐ (2020 ์นด์นด์˜ค ์ธํ„ด์‹ญ ๋ฌธ์ œ)

๊ฐ์ž ๐Ÿฅ” 2021. 9. 8. 19:33
๋ฐ˜์‘ํ˜•

 

๋ฌธ์ œ ๋งํฌ

https://programmers.co.kr/learn/courses/30/lessons/6725

 

๋‚˜์˜ ํ’€์ด

  • ์ˆซ์ž๋ฅผ ์ขŒํ‘œ๋กœ ๋งŒ๋“ค์–ด์„œ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ•œ๋‹ค๋Š”๊ฑธ ๋„ˆ๋ฌด๋Šฆ๊ฒŒ ์ƒ๊ฐํ•ด๋ƒ„
  • ์ด๊ฑฐ ์ƒ๊ฐ๋งŒ ํ•ด๋‚ด๋ฉด just ๊ตฌํ˜„์œผ๋กœ ๊ฐ„๋‹จํ•˜๊ฒŒ ํ•ด๊ฒฐ ๊ฐ€๋Šฅํ•œ ๋ฌธ์ œ
  • ์•ž์œผ๋กœ ์ผ์ •ํ•œ ํ˜•ํƒœ, ๋ณ€ํ™”๊ฐ€ ์—†๋Š” ๊ณ ์ •๋œ ์ž๋ฆฌ?๋ฅผ ์ด์šฉํ•˜๋Š” ๋ฌธ์ œ๋Š” ์˜์‹ฌํ•˜์ง€๋ง๊ณ  ์ขŒํ‘œ๋ฅผ ์ด์šฉํ•˜์ž
  1. ๋จผ์ € ํ‚คํŒจ๋“œ๋ฅผ ๋”•์…”๋„ˆ๋ฆฌํ˜•ํƒœ๋กœ key๊ฐ’์€์ˆซ์ž, value๊ฐ’์€ ์ขŒํ‘œ์˜ ์œ„์น˜๋กœ ํ‘œํ˜„
  2. ํ˜„์žฌ ์†์˜ ์œ„์น˜ (left now, right now)๋ฅผ ์ •ํ•ด์ฃผ๊ณ  
  3. 1,4,7์ด ๋‚˜์˜ค๋ฉด ์—ฌ์ง€์—†์ด Left ๊ณ , 3, 6, 9๊ฐ€ ๋‚˜์˜ค๋ฉด ์—ฌ์ง€์—†์ด Right๋‹ˆ๊นŒ if๋ฌธ์œผ๋กœ๊ตฌํ˜„
  4. 2, 5, 8, 0์ด ๋‚˜์˜ค๋ฉด ์†์˜ ์œ„์น˜์— ๋”ฐ๋ผ์„œ Left / Right๋ฅผ ์ •ํ•ด์ฃผ์–ด์•ผ ํ•˜๊ธฐ๋•Œ๋ฌธ์— ์ขŒํ‘œ์˜ ์œ„์น˜๋ฅผ ํ™œ์šฉํ•ด์„œ ๋งจํ•˜ํƒ„ ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ•œ๋’ค, ๋” ๊ฐ€๊นŒ์šด ์†์„ ์ง€์ •
  5. for๋ฌธ์ด ๋Œ์•„๊ฐˆ ๋•Œ๋งˆ๋‹ค now์˜ ์œ„์น˜๋ฅผ ๋ฐ”๊พธ์–ด์ฃผ๋ฉด์„œ ์ฝ”๋“œ ์ง„ํ–‰
def solution(numbers, hand):
    answer = ""
    # ํ‚คํŒจ๋“œ ์ˆซ์ž๋ฅผ ์ขŒํ‘œ๋กœ ํ‘œํ˜„
    dic = {1:[0,0], 2:[0,1], 3:[0,2],
          4:[1,0], 5:[1,1], 6:[1,2],
          7:[2,0], 8:[2,1], 9:[2,2],
          10:[3,0], 0:[3,1], 11:[3,2]}
    left_now = dic[10] #ํ˜„์žฌ ์™ผ์† ์œ„์น˜ (10์€*)
    right_now = dic[11] #ํ˜„์žฌ ์˜ค๋ฅธ์† ์œ„์น˜ (11๋Š” #)

    for num in numbers:
        if num in [1, 4, 7]:
            answer += "L"
            left_now = dic[num]
        elif num in [3, 6, 9]:
            answer += "R"
            right_now = dic[num]
        else:
            now = dic[num]
            # ๊ทธ๋ž˜ํ”„๋‹ˆ๊นŒ ๊ทธ๋ƒฅ ๋งจํ•˜ํƒ„๊ฑฐ๋ฆฌ ์ด์šฉ
            left_dis = abs(now[0]-left_now[0]) + abs(now[1]-left_now[1])
            right_dis = abs(now[0]-right_now[0]) + abs(now[1]-right_now[1])
            if left_dis > right_dis: # ๊ฐ€๊นŒ์šด์†์œผ๋กœ ํ„ฐ์น˜
                answer += "R"
                right_now = dic[num]
            elif left_dis < right_dis:
                answer += "L"
                left_now = dic[num]
            elif left_dis == right_dis: #๊ฐ™์œผ๋ฉด ์™ผ์†์žก์ด,์˜ค๋ฅธ์†์žก์ดํŒ๋‹จ 
                if hand == "right":
                    answer += "R"
                    right_now = dic[num]
                else:
                    answer += "L"
                    left_now = dic[num]
    return answer

 

๋‹ค๋ฅธ ์‚ฌ๋žŒ ํ’€์ด

https://programmers.co.kr/learn/courses/30/lessons/67256/solution_groups?language=python3&type=all 

 

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

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

programmers.co.kr

๋‚˜๋ž‘ ๋น„์Šทํ•˜๊ฒŒ ํ’€์—ˆ๋‹ค! ๋‚˜๋„ ์ขŒํ‘œ๋ฅผ ์ƒ๊ฐํ•ด ๋‚ด๋Š” ๊ฒƒ์— ์‹œ๊ฐ„์ด ์กฐ๊ธˆ ๊ฑธ๋ ธ๋Š”๋ฐ, ์ด๋Ÿฐ ์œ ํ˜•์€ ์ด์ œ ๋งŒ๋‚˜๋ฉด ๋น ๋ฅด๊ฒŒ ํ’€ ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™๋‹ค. ์ด๋ ‡๊ฒŒ ๊ณ ์ •๋œ ํ‚คํŒจ๋“œ์™€ ๊ฐ™์€ ์œ„์น˜์—์„œ ์›€์ง์ด๋Š” ๋ฌธ์ œ์ผ ๊ฒฝ์šฐ ๋ฌด์กฐ๊ฑด ์ขŒํ‘œ๋ฅผ ์ด์šฉํ•ด๋ด์•ผ๊ฒ ๋‹ค!

 

๋ฐ˜์‘ํ˜•