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

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

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ํ‚คํŒจ๋“œ ๋ˆ„๋ฅด๊ธฐ (Python)

1. ๋ฌธ์ œ ์„ค๋ช…

๋ฌธ์ œ ๋งํฌ

  • ์ž…๋ ฅ :
    • numbers [1, 3, 4, 5, 8, 2, 1, 4, 5, 9, 5] : ๋ˆ„๋ฅผ ๋ฒˆํ˜ธ๋ฅผ ์ˆœ์„œ๋Œ€๋กœ
    • hand "right" : ์˜ค๋ฅธ์†์žก์ด์ธ์ง€, ์™ผ์†์žก์ด์ธ์ง€
  • ์ถœ๋ ฅ :
    • "LRLLLRLLRRL"
    • 1,4,7 ์€ ์™ผ์†์œผ๋กœ ๋ˆ„๋ฅด๊ณ , 3,6,9๋Š” ์˜ค๋ฅธ์†์œผ๋กœ ๋ˆ„๋ฅธ๋‹ค.
    • ๊ทธ๋ฆฌ๊ณ  ๋‚˜๋จธ์ง€ ์ˆซ์ž๋“ค์€ ๋‘ ์—„์ง€์†๊ฐ€๋ฝ์˜ ํ˜„์žฌ ํ‚คํŒจ๋“œ์˜ ์œ„์น˜์—์„œ ๋” ๊ฐ€๊นŒ์šด ์—„์ง€์†๊ฐ€๋ฝ์„ ์‚ฌ์šฉ
    • ๋งŒ์•ฝ ๋‘ ์—„์ง€์†๊ฐ€๋ฝ์˜ ๊ฑฐ๋ฆฌ๊ฐ€ ๊ฐ™๋‹ค๋ฉด, ์˜ค๋ฅธ์†์žก์ด๋Š” ์˜ค๋ฅธ์† ์—„์ง€์†๊ฐ€๋ฝ, ์™ผ์†์žก์ด๋Š” ์™ผ์† ์—„์ง€์†๊ฐ€๋ฝ์„ ์‚ฌ์šฉ
    • ๊ฐ ๋ฒˆํ˜ธ(numbers)๋ฅผ ๋ˆ„๋ฅธ ์—„์ง€์†๊ฐ€๋ฝ์ด ์™ผ์†์ธ ์ง€ ์˜ค๋ฅธ์†์ธ ์ง€๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์—ฐ์†๋œ ๋ฌธ์ž์—ด ํ˜•ํƒœ๋กœ return

2. ์ฝ”๋“œ

solution1.py

def check_near_hand(current_position,num):
    positions = {
        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)
    }

    left_hand = positions[current_position[0]]
    right_hand = positions[current_position[1]]
    num_position = positions[num]
    #์™ผ์†๊ณผ Num์˜ ๊ฑฐ๋ฆฌ
    left_distance = abs(left_hand[0] - num_position[0]) + abs(left_hand[1]-num_position[1])
    #์˜ค๋ฅธ์†๊ณผ Num์˜ ๊ฑฐ๋ฆฌ
    right_distance = abs(right_hand[0] - num_position[0]) + abs(right_hand[1]-num_position[1])

    if left_distance < right_distance:
        return 'L'
    elif left_distance > right_distance:
        return 'R'
    else:
        return 'hand'

def solution(numbers, hand):
    answer = ''
    current_position = ['*','#']

    left_key = [1,4,7]
    right_key = [3,6,9]

    for num in numbers:
        if num in left_key:
            current_position[0] = num
            answer +='L'
        elif num in right_key:
            current_position[1] = num
            answer +='R'
        else:
            if check_near_hand(current_position,num) == 'L':
                current_position[0] = num
                answer +='L'
            elif check_near_hand(current_position,num) == 'R':
                current_position[1] = num
                answer +='R'
            else:
                if hand == 'left':
                    current_position[0] = num
                    answer +='L'
                else:
                    current_position[1] = num
                    answer +='R'
    return answer

solution2.py

def check_near_hand(l,r,num, hand):
    positions = {
        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)
    }

    left_distance = abs(positions[l][0] - positions[num][0]) + abs(positions[l][1]-positions[num][1])
    right_distance = abs(positions[r][0] - positions[num][0]) + abs(positions[r][1]-positions[num][1])

    if left_distance == right_distance:
        return 'L' if hand == 'left' else 'R'
    return 'L' if left_distance < right_distance else 'R'


def solution(numbers, hand):
    answer = ''
    current_position = ['*','#']

    left_key = [1,4,7]
    right_key = [3,6,9]

    for num in numbers:
        if num in left_key:
            current_position[0] = num
            answer +='L'
        elif num in right_key:
            current_position[1] = num
            answer +='R'
        else:
            if check_near_hand(current_position[0],current_position[1],num,hand) == 'L':
                current_position[0] = num
                answer +='L'
            else:
                current_position[1] = num
                answer +='R'

    return answer

3. ํšŒ๊ณ 

  • ๊ฑฐ๋ฆฌ๊ณ„์‚ฐ์„ ์–ด๋–ป๊ฒŒ ํ•  ์ง€๊ฐ€ ๊ฐ€์žฅ ๊ณ ๋ฏผ์ด์—ˆ๋‹ค.
  • ๋”•์…”๋„ˆ๋ฆฌ๋กœ ์ขŒํ‘œ๋ฅผ ์ •ํ•ด์ฃผ๊ณ  abs(์™ผ์† ํ˜„์žฌ์œ„์น˜ x๊ฐ’ - ๋ˆ„๋ฅด๋ ค๋Š” ๋ฒˆํ˜ธ์œ„์น˜ x๊ฐ’) + abs(์™ผ์† ํ˜„์žฌ์œ„์น˜ y๊ฐ’ - ๋ˆ„๋ฅด๋ ค๋Š” ๋ฒˆํ˜ธ์œ„์น˜ y๊ฐ’) ์œผ๋กœ ์™ผ์†๊ณผ ๋ˆ„๋ฅด๋ ค๋Š” ๋ฒˆํ˜ธ์˜ ๊ฑฐ๋ฆฌ, ์˜ค๋ฅธ์†๋„ ๋งˆ์นœ๊ฐ€์ง€๋กœ ํ•ด์„œ ๋‘˜์„ ๋น„๊ตํ–ˆ๋‹ค.
  • ์ฒซ ๋ฒˆ์งธ ํ’€์ด์—์„œ๋Š” ๊ฑฐ๋ฆฌ๊ณ„์‚ฐ ๊ณผ์ •์—์„œ ํ—ท๊ฐˆ๋ ค์„œ ์—ฌ๋Ÿฌ ๋ณ€์ˆ˜๋ฅผ ์ง€์ •ํ•˜์—ฌ ์ผ๋‹ค.
  • ๊ทธ๋ž˜์„œ ๋‘ ๋ฒˆ์งธ ํ’€์ด์—์„œ๋Š” current_position ๋ฐฐ์—ด ์ž์ฒด๋ณด๋‹ค ์™ผ์†ํ˜„์žฌ์œ„์น˜, ์˜ค๋ฅธ์†ํ˜„์žฌ์œ„์น˜๋ฅผ ๋”ฐ๋กœ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ „๋‹ฌํ–ˆ๋‹ค.
  • ๋‘ ๋ฒˆ์งธ ํ’€์ด๋Š” if ๋ฌธ์ด ๋„ˆ๋ฌด ๋งŽ์ด ์“ฐ์ธ ๊ฒƒ ๊ฐ™์•„์„œ ํ•œ์ค„๋กœ ์ •๋ฆฌํ–ˆ๋‹ค.
  • ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ๋ฌธ์ œ์— ์ ํ˜€์žˆ์–ด์„œ ์ด๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ ํฐ ํ‹€์„ ์žก๊ณ  ํ•„์š”ํ•œ ํ•จ์ˆ˜๋ฅผ ๋‚˜์ค‘์— ์ž‘์„ฑํ•ด๋‚˜๊ฐ€๋Š” ๊ฒƒ์œผ๋กœ ๋ฐฉํ–ฅ์„ ์žก์€ ๊ฒƒ์ด ๋„์›€์ด ๋˜์—ˆ๋‹ค.