[Algorithm] 프로그래머스 키패드 누르기

Updated:

문제 설명

5

나의 풀이

import Foundation
func solution(_ numbers:[Int], _ hand:String) -> String {
    let dictionary = [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),
                 0:(3,1)]
    var leftHandLoc = [3,0]
    var rightHandLoc = [3,2]
    var answer = ""
    
    var currentNumberLoc = [0,0]
    var leftHandDistance = 0
    var rightHandDistance = 0
    
    for num in numbers {
        if num == 1 || num == 4 || num == 7 {
            if num == 1 {
                leftHandLoc = [0,0]
            }
            else if num == 4 {
                leftHandLoc = [1,0]
            }
            else {
                leftHandLoc = [2,0]
            }
            answer += "L"
        } else if num == 3 || num == 6 || num == 9 {
            if num == 3 {
                rightHandLoc = [0,2]
            }
            else if num == 6 {
                rightHandLoc = [1,2]
            }
            else {
                rightHandLoc = [2,2]
            }
            answer += "R"
        } else {
            currentNumberLoc[0] = dictionary[num]!.0
            currentNumberLoc[1] = dictionary[num]!.1
            leftHandDistance = abs(currentNumberLoc[0]-leftHandLoc[0]) + abs(currentNumberLoc[1]-leftHandLoc[1])
            rightHandDistance = abs(currentNumberLoc[0]-rightHandLoc[0]) + abs(currentNumberLoc[1]-rightHandLoc[1])
            
            if leftHandDistance > rightHandDistance {
                rightHandLoc = currentNumberLoc
                answer += "R"
            } else if leftHandDistance < rightHandDistance {
                leftHandLoc = currentNumberLoc
                answer += "L"
            } else {
                if hand == "left" {
                    leftHandLoc = currentNumberLoc
                    answer += "L"
                } else {
                    rightHandLoc = currentNumberLoc
                    answer += "R"
                }
            }
        }
    }
    
    return answer
}

고민한 점

Swift tuple 타입의 원소 접근 방법은 다음과 같았다.

// tupleExample = (a,b) 라고 할 때 
// tupleExample.0 = a
// tupleExample.1 = b

괜찮은 남의 풀의

import Foundation
func solution(_ numbers:[Int], _ hand:String) -> String {
    // 버튼의 위치 테이블
    let buttonPoses = [1: Position(x: 0, y: 0), 2: Position(x: 1, y: 0), 3: Position(x: 2, y: 0),
                       4: Position(x: 0, y: 1), 5: Position(x: 1, y: 1), 6: Position(x: 2, y: 1),
                       7: Position(x: 0, y: 2), 8: Position(x: 1, y: 2), 9: Position(x: 2, y: 2),
                       0: Position(x: 1, y: 3)]
    var leftThumbPos = Position(x: 0, y: 3)     // 왼손 엄지는 *에서 시작
    var rightThumbPos = Position(x: 2, y: 3)    // 오른손 엄지는 #에서 시작
    var answer = ""

    for number in numbers {
        // 키를 누르는데 사용할 엄지 손가락 결정
        let selectedHand: String
        switch number {
            case 1, 4, 7:
                selectedHand = "L"
            case 3, 6, 9:
                selectedHand = "R"
            default:
                let targetButtonPos = buttonPoses[number]!
                let leftThumbDistance = targetButtonPos.manhattanDistance(to: leftThumbPos)
                let rightThumbDistance = targetButtonPos.manhattanDistance(to: rightThumbPos)
                if leftThumbDistance < rightThumbDistance {
                    selectedHand = "L"
                } else if rightThumbDistance < leftThumbDistance {
                    selectedHand = "R"
                } else {
                    selectedHand = hand == "left" ? "L" : "R"
                }
        }

        // 버튼을 누른 손가락을 이동시키고 사용한 손가락을 기록
        let buttonPos = buttonPoses[number]!
        if selectedHand == "L" {
            leftThumbPos = buttonPos
        } else {
            rightThumbPos = buttonPos
        }
        answer += selectedHand
    }

    return answer
}

struct Position {
    var x: Int
    var y: Int

    func manhattanDistance(to: Position) -> Int {
        return abs(x - to.x) + abs(y - to.y)
    }
}

풀이방식은 결국 똑같지만 Struct를 생성하고 내부 함수로 좀 더 코드를 깔끔하게 작성한 것 같다!

참고

Programmers

Leave a comment