본문 바로가기
전공공부/코딩테스트

(c++) 프로그래머스 키패드 누르기

by 시아나 2022. 1. 6.

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

 


나의 풀이

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

using namespace std;

string solution(vector<int> numbers, string hand) {
    string answer = "";
    int right, left; 
    right = -2; left = -1;
    unordered_map<int, pair<int, int>> site;
    for (int i = 0; i < 3; i++) {
        for (int k = 1; k < 4; k++) {
            site[(3 * i) + k] = { i,k - 1 };
        }
    }
    site[-1] = { 3,0};
    site[-2] = { 3,2 };
    site[0] = { 3,1 };
    for (int num : numbers) {
        switch (num) {
        case 1: case 4: case 7:
            answer += "L";
            left = num;
            break;
        case 3 : case 6 : case 9 :
            answer += 'R';
            right = num;
            break;
        default:
            pair<int, int> go = site.find(num)->second;
            int lenLeft = max(site.find(left)->second.first,go.first) - min(site.find(left)->second.first,go.first);
            lenLeft += max(site.find(left)->second.second, go.second) - min(site.find(left)->second.second, go.second);
            int lenRight = max(site.find(right)->second.first, go.first) - min(site.find(right)->second.first, go.first);
            lenRight += max(site.find(right)->second.second, go.second) - min(site.find(right)->second.second, go.second);
            if (lenLeft == lenRight) {
                if (hand == "right") {
                    answer += "R";
                    right = num;
                }
                else {
                    answer += "L";
                    left = num;
                }
            }
            else if (lenLeft < lenRight) {
                answer += "L";
                left = num;
            }
            else {
                answer += "R";
                right = num;
            }
            break;
        }
    }

    return answer;
}