😎풀이

  1. commands 순회
    1-1. UP, RIGHT, DOWN, LEFT 명령에 따른 X, Y 좌표 변경
  2. N * Y + X의 점화식이 Array[Y][X]의 값이므로, 결괏값 반환
function finalPositionOfSnake(n: number, commands: string[]): number {
    let [y, x] = [0, 0]
    for(const command of commands) {
        switch(command) {
            case 'UP':
                y--
                break
            case 'RIGHT':
                x++
                break
            case 'DOWN':
                y++
                break
            case 'LEFT':
                x--
                break
        }
    }
    return y * n + x
};
profile
내 지식을 공유할 수 있는 대담함

0개의 댓글