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

Algorithm/Programmers

[Programmers] ํ‚คํŒจ๋“œ ๋ˆ„๋ฅด๊ธฐ

 

 

 

 

์ฝ”๋“œ 

 

def keypad(current_N,next_N):
    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],'*':[3,0],'0':[3,1],'#':[3,2]}
    
    x1,y1 = dic[current_N]
    x2,y2 = dic[next_N]
    
    return abs(int(x1) - int(x2)) + abs(int(y1) - int(y2))

def solution(numbers,hand):
    ans = ''
    left = '*'
    right = '#'
    for i in numbers:
        if i in [1,4,7]:
            left = str(i)
            ans += 'L'
        elif i in [3,6,9]:
            right = str(i)
            ans += 'R'
        else:
            if keypad(left,str(i)) > keypad(right,str(i)):
                right = str(i)
                ans += 'R'
            elif keypad(left,str(i)) < keypad(right,str(i)):
                left = str(i)
                ans += 'L'
            else:
                if hand == 'left':
                    left = str(i)
                    ans += 'L'
                else:
                    right = str(i)
                    ans += 'R'
    return ans
๋ถ„์„ > ์ฒ˜์Œ์œผ๋กœ ๋‘ ๊ฐœ์˜ ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด ์ฝ”๋“œ๋ฅผ ์งœ๋ดค๋‹ค. ์ด ๋ฌธ์ œ์—์„œ์˜ ๋‚œ์ œ๋Š” ์–ด๋–ป๊ฒŒ ํ‚คํŒจ๋“œ ์‚ฌ์ด์˜ ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ•˜๋ƒ์˜€๋‹ค. ๊ทธ๋ž˜์„œ ํ‚คํŒจ๋“œ ์‚ฌ์ด์˜ ๊ฑฐ๋ฆฌ๋ฅผ ์ฐพ๋Š” ํ•จ์ˆ˜๋ฅผ ๋”ฐ๋กœ ๋งŒ๋“ค์—ˆ๋‹ค. ํ‚คํŒจ๋“œ์˜ ์œ„์น˜๋Š” ์ขŒํ‘œ๋กœ ๋งŒ๋“ค์–ด ํ•ด์‹œ๋งต์˜ value๊ฐ’์œผ๋กœ ์ง€์ •ํ•˜์—ฌ ํ‚คํŒจ๋“œ ์‚ฌ์ด์˜ ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ•˜์˜€๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋‚˜๋จธ์ง€ ์กฐ๊ฑด์€ ๋ฌธ์ œ์— ๋‚˜์˜จ๋Œ€๋กœ ๊ทธ๋Œ€๋กœ ๊ตฌํ˜„ํ•˜๋ฉด ๋˜๋Š” ๋ฌธ์ œ์˜€๋‹ค. 

 

 

 

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ํ‚คํŒจ๋“œ ๋ˆ„๋ฅด๊ธฐ

[1, 3, 4, 5, 8, 2, 1, 4, 5, 9, 5] "right" "LRLLLRLLRRL" [7, 0, 8, 2, 8, 3, 1, 5, 7, 6, 2] "left" "LRLLRRLLLRR" [1, 2, 3, 4, 5, 6, 7, 8, 9, 0] "right" "LLRLLRLLRL"

programmers.co.kr

 

'Algorithm > Programmers' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[Programmers] ๊ตฌ๋ช…๋ณดํŠธ  (0) 2022.05.19
[Programmers] ์†Œ์ˆ˜ ์ฐพ๊ธฐ  (0) 2022.05.19
[Programmers] ์œ„์žฅ  (0) 2022.05.18
[Programmers] K๋ฒˆ์งธ ์ˆ˜  (0) 2022.05.07
[Programmers] ์‹คํŒจ์œจ  (0) 2022.05.07