프로그래밍 공부
작성일
2024. 5. 26. 11:13
작성자
WDmil
728x90

문제 설명

스마트폰 전화 키패드의 각 칸에 다음과 같이 숫자들이 적혀 있습니다.

 

 

이 전화 키패드에서 왼손과 오른손의 엄지손가락만을 이용해서 숫자만을 입력하려고 합니다.

맨 처음 왼손 엄지손가락은 * 키패드에 오른손 엄지손가락은 # 키패드 위치에서 시작하며, 엄지손가락을 사용하는 규칙은 다음과 같습니다.

 

  1. 엄지손가락은 상하좌우 4가지 방향으로만 이동할 수 있으며 키패드 이동 한 칸은 거리로 1에 해당합니다.
  2. 왼쪽 열의 3개의 숫자 1, 4, 7을 입력할 때는 왼손 엄지손가락을 사용합니다.
  3. 오른쪽 열의 3개의 숫자 3, 6, 9를 입력할 때는 오른손 엄지손가락을 사용합니다.
  4. 가운데 열의 4개의 숫자 2, 5, 8, 0을 입력할 때는 두 엄지손가락의 현재 키패드의 위치에서 더 가까운 엄지손가락을 사용합니다.
    1. 만약 두 엄지손가락의 거리가 같다면, 오른손잡이는 오른손 엄지손가락, 왼손잡이는 왼손 엄지손가락을 사용합니다.

 

순서대로 누를 번호가 담긴 배열 numbers, 왼손잡이인지 오른손잡이인 지를 나타내는 문자열 hand가 매개변수로 주어질 때, 각 번호를 누른 엄지손가락이 왼손인 지 오른손인 지를 나타내는 연속된 문자열 형태로 return 하도록 solution 함수를 완성해주세요.

 

[제한사항]

  • numbers 배열의 크기는 1 이상 1,000 이하입니다.
  • numbers 배열 원소의 값은 0 이상 9 이하인 정수입니다.
  • hand는 "left" 또는 "right" 입니다.
  • "left"는 왼손잡이, "right"는 오른손잡이를 의미합니다.
  • 왼손 엄지손가락을 사용한 경우는 L, 오른손 엄지손가락을 사용한 경우는 R을 순서대로 이어붙여 문자열 형태로 return 해주세요.

입출력 예

numbers hand result
[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"

문제 해설

 

조건을 살펴보고 정리해보자.

 

  • 먼저, 엄지손가락은 상하 좌우 4가지 방향만 이동할 수 있다.
  • 왼쪽 열의 숫자는 항상 왼손을 사용한다.
  • 오른쪽 열의 숫자는 항상 오른손 을 사용한다.
  • 가운데 열의 4개 숫자는 두 엄지 손가락을 기준으로 가까운 엄지손가락을 사용한다.

 

이걸 코드로 구현하기 위해 다시 정리하면 다음과 같이 정리할 수 있다.

 

  • 먼저, 각 키패드의 위치를 xy좌표도로 정리한다.
  • 엄지 손가락의 거리측정방식은 멘하탄 방식의 거리측정방식을 사용해야 한다. 4방향으로 이동하기 때문이다.
  • 왼쪽, 오른쪽은 담당하는 손가락의 위치만 갱신해준다.
  • 중앙의 측정방식에, 멘하탄 방식의 거리측정을 사용하고 같을경우 주어진 손으로 갱신해준다.

 

유클리드 방식이 아닌 멘하탄 방식의 거리측정방식 을 사용하도록 유의하자.


첫 번째 시도

#include <string>
#include <vector>
#include <unordered_map>
#include <cmath>

using namespace std;

typedef string (*FuncPtr)(const vector<int>&,vector<int>&,vector<int>&, const string&);

string InputL(const vector<int>& numberVector2D, vector<int>& handR, vector<int>& handL, const string& hand)
{
    handL = numberVector2D;
    return "L";
}
string InputR(const vector<int>& numberVector2D, vector<int>& handR, vector<int>& handL, const string& hand)
{
    handR = numberVector2D;
    return "R";
}

float GetDistance(const vector<int>& A, const vector<int>& B)
{
    return sqrt(pow(A[0] - B[0], 2) + pow(A[1] - B[1], 2));
}

string ChackDistance(const vector<int>& numberVector2D, vector<int>& handR, vector<int>& handL, const string& hand)
{
    float distanceR = GetDistance(numberVector2D, handR);
    float distanceL = GetDistance(numberVector2D, handL);
    
    if (distanceR < distanceL) {
        handR = numberVector2D;
        return "R";
    }
    else if (distanceR > distanceL) {
        handL = numberVector2D;
        return "L";
    }
    else if (hand[0] == 'l') {
        handL = numberVector2D;
        return "L";
    }
    else if (hand[0] == 'r') {
        handR = numberVector2D;
        return "R";
    }
}

string solution(vector<int> numbers, string hand) {
    string answer = "";
    unordered_map<int, vector<int>> PointAndVector2D;
    unordered_map<int, FuncPtr> PointAndFuntion;

    for (int i = 0; i < 9; i++) PointAndVector2D[i+1] = { i / 3, i % 3};
    PointAndVector2D[0] = { 1, 3 };

    PointAndFuntion[0] = ChackDistance;
    PointAndFuntion[1] = InputL;
    PointAndFuntion[2] = ChackDistance;
    PointAndFuntion[3] = InputR;
    PointAndFuntion[4] = InputL;
    PointAndFuntion[5] = ChackDistance;
    PointAndFuntion[6] = InputR;
    PointAndFuntion[7] = InputL;
    PointAndFuntion[8] = ChackDistance;
    PointAndFuntion[9] = InputR;

    vector<int> StartL = {0, 3};
    vector<int> StartR = {2, 3};

    for (auto& def : numbers)
        answer += PointAndFuntion[def](PointAndVector2D[def], StartR, StartL, hand);

    return answer;
}

실패

 

어차피 숫자의 개수는 정해져있기 때문에, 좌표도와 함수포인터 를 정리하고,

지정된 좌표의 해시키값에 정해진 함수를 밀어넣으면 된다.

 

그러면, numbers의 탐색시간은 O(N)을 보장한다.

 

유클리드 방식의 거리측정을 사용했다. 이걸 멘하탄 거리측정방식 으로 바꾸면 성공한다.


두 번째 시도

#include <string>
#include <vector>
#include <unordered_map>
#include <cmath>

using namespace std;

typedef string (*FuncPtr)(const vector<int>&,vector<int>&,vector<int>&, const string&);

string InputL(const vector<int>& numberVector2D, vector<int>& handR, vector<int>& handL, const string& hand)
{
    handL = numberVector2D;
    return "L";
}
string InputR(const vector<int>& numberVector2D, vector<int>& handR, vector<int>& handL, const string& hand)
{
    handR = numberVector2D;
    return "R";
}

float GetDistance(const vector<int>& A, const vector<int>& B)
{
    return abs(A[0] - B[0]) + abs(A[1] - B[1]);
}

string ChackDistance(const vector<int>& numberVector2D, vector<int>& handR, vector<int>& handL, const string& hand)
{
    float distanceR = GetDistance(numberVector2D, handR);
    float distanceL = GetDistance(numberVector2D, handL);
    
    if (distanceR < distanceL) {
        handR = numberVector2D;
        return "R";
    }
    else if (distanceR > distanceL) {
        handL = numberVector2D;
        return "L";
    }
    else if (hand[0] == 'l') {
        handL = numberVector2D;
        return "L";
    }
    else if (hand[0] == 'r') {
        handR = numberVector2D;
        return "R";
    }
}

string solution(vector<int> numbers, string hand) {
    string answer = "";
    unordered_map<int, vector<int>> PointAndVector2D;
    unordered_map<int, FuncPtr> PointAndFuntion;

    for (int i = 0; i < 9; i++) PointAndVector2D[i+1] = { i % 3, i / 3};
    PointAndVector2D[0] = { 1, 3 };

    PointAndFuntion[0] = ChackDistance;
    PointAndFuntion[1] = InputL;
    PointAndFuntion[2] = ChackDistance;
    PointAndFuntion[3] = InputR;
    PointAndFuntion[4] = InputL;
    PointAndFuntion[5] = ChackDistance;
    PointAndFuntion[6] = InputR;
    PointAndFuntion[7] = InputL;
    PointAndFuntion[8] = ChackDistance;
    PointAndFuntion[9] = InputR;

    vector<int> StartL = {0, 3};
    vector<int> StartR = {2, 3};

    for (auto& def : numbers)
        answer += PointAndFuntion[def](PointAndVector2D[def], StartR, StartL, hand);

    return answer;
}

성공

멘하탄 방식을 사용했다.

 

4방향이기 때문에 대각선을 고려하지 않아도 된다.

728x90

'코딩테스트 문제 풀이 > 해시' 카테고리의 다른 글

영어 끝말잇기  (1) 2024.04.03
스킬트리  (0) 2024.04.01
방문 길이  (0) 2024.03.29
베스트앨범  (0) 2024.02.13
의상  (0) 2024.02.07