๐Ÿ—๏ธ Algorithm/โฌ› ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

[Programmers] [2020 ์นด์นด์˜ค ์ธํ„ด์‹ญ] [Python] Level1_ํ‚คํŒจ๋“œ ๋ˆ„๋ฅด๊ธฐ

Dbswnstjd 2022. 3. 7. 18:39

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

 

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

[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

ํ’€์ด

# ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค 1๋‹จ๊ณ„ - ํ‚คํŒจ๋“œ ๋ˆ„๋ฅด๊ธฐ
def solution(numbers, hand):
    answer = ''
    left_hand_location = 10
    right_hand_location = 12
    for i in numbers: 
        if i in [1,4,7]:
            answer += "L"
            left_hand_location = i
        elif i in [3,6,9]:
            answer += "R"
            right_hand_location = i
        else:
            i = 11 if i == 0 else i 
            dis_left = abs(left_hand_location - i)
            dis_right = abs(right_hand_location - i)
        
            if sum(divmod(dis_left, 3)) > sum(divmod(dis_right, 3)):
                answer += "R"
                right_hand_location = i
            elif sum(divmod(dis_left, 3)) < sum(divmod(dis_right, 3)):
                answer += "L"
                left_hand_location = i
            else:
                if hand == 'left':
                    answer += "L"
                    left_hand_location = i
                else:
                    answer += "R"
                    right_hand_location = i
    return answer

print(solution([1, 3, 4, 5, 8, 2, 1, 4, 5, 9, 5],"right"))