나의풀이-무식한방법

import java.lang.*;
class Solution {
    public String solution(int[] numbers, String hand) {
        String answer = "";
        int lefthand=10; int righthand=11;
        char [][]phone={{1,2,3},{4,5,6},{7,8,9},{'*',0,'#'}};
        int [][] gps = new int[12][12];
        gps[1][2]=1;gps[1][5]=2;gps[1][8]=3;gps[1][0]=4;
        gps[4][2]=2;gps[4][5]=1;gps[4][8]=2;gps[4][0]=3;
        gps[7][2]=3;gps[7][5]=2;gps[7][8]=1;gps[7][0]=2;
        gps[3][2]=1;gps[3][5]=2;gps[3][8]=3;gps[3][0]=4;
        gps[6][2]=2;gps[6][5]=1;gps[6][8]=2;gps[6][0]=3;
        gps[9][2]=3;gps[9][5]=2;gps[9][8]=1;gps[9][0]=2;
        gps[2][5]=1;gps[2][8]=2;gps[2][0]=3;
        gps[5][2]=1;gps[5][8]=1;gps[5][0]=2;
        gps[8][2]=2;gps[8][5]=1;gps[8][0]=1;
        gps[0][2]=3;gps[0][5]=2;gps[0][8]=1;
        gps[10][2]=4;gps[10][5]=3;gps[10][8]=2;gps[10][0]=1;
        gps[11][2]=4;gps[11][5]=3;gps[11][8]=2;gps[11][0]=1;
        for(int l=0;l<12;l++){
            {gps[l][l]=0;}
        }
        
        for(int i:numbers){   
        if(i==1||i==4||i==7)
        {answer+='L';
        lefthand=i;
        }
        if(i==3||i==6||i==9)
        {answer+='R';
        righthand=i;
        }
        if(i==2||i==5||i==8||i==0)
        {   
            if(gps[lefthand][i]  >  gps[righthand][i])
            { answer+='R'; righthand=i;}
            else if (gps[lefthand][i]  < gps[righthand][i])
            {answer+='L';lefthand=i;}
            else{ 
                if(hand.equals("right"))
                {answer+='R';righthand=i;}
                else
                {answer+='L';lefthand=i;}
             }
        
        }
        }
    return answer;
    }
}

다른풀이- hashmap사용

import java.util.HashMap;

class Solution {
      public static String solution(int[] numbers, String hand) {
            String answer = "";
            HashMap<Integer, int []> hm = new HashMap<>();
            hm.put(0, new int[] {3,1});
            hm.put(1, new int[] {0,0});
            hm.put(2, new int[] {0,1});
            hm.put(3, new int[] {0,2});
            hm.put(4, new int[] {1,0});
            hm.put(5, new int[] {1,1});
            hm.put(6, new int[] {1,2});
            hm.put(7, new int[] {2,0});
            hm.put(8, new int[] {2,1});
            hm.put(9, new int[] {2,2});
            int lx=0,ly=3,rx=2,ry=3;
            for(int i = 0 ; i < numbers.length ; i++) {
                int [] now = hm.get(numbers[i]);
                if(now[1] ==0) {
                    answer +="L";
                    lx = now[1];
                    ly = now[0];
                } else if(now[1] ==2) {
                    answer +="R";
                    rx = now[1];
                    ry = now[0];

                } else {
                    // 거리계산
                    if(Math.abs(lx - now[1]) + Math.abs(ly - now[0]) >  Math.abs(rx - now[1]) + Math.abs(ry - now[0]) ) {
                        // 오른쪽이 가까운경우
                        rx = now[1]; ry = now[0];
                        answer +="R";
                    } else if( Math.abs(lx - now[1]) + Math.abs(ly - now[0]) <  Math.abs(rx - now[1]) + Math.abs(ry - now[0]) ) {
                        lx = now[1]; ly = now[0];
                        answer +="L";
                    } else {
                        char t = Character.toUpperCase(hand.charAt(0));
                        if(t == 'R') {
                            rx = now[1] ; ry = now[0];
                        } else {
                            lx = now[1] ; ly = now[0];
                        }
                        answer += t;
                    }
                }
            }

            return answer;
        }
}

다른풀이-

class Solution {
    //        0부터 9까지 좌표 {y,x}
    int[][] numpadPos = {
            {3,1}, //0
            {0,0}, //1
            {0,1}, //2
            {0,2}, //3
            {1,0}, //4
            {1,1}, //5
            {1,2}, //6
            {2,0}, //7
            {2,1}, //8
            {2,2}  //9
    };
    //초기 위치
    int[] leftPos = {3,0};
    int[] rightPos = {3,2};
    String hand;
    public String solution(int[] numbers, String hand) {
        this.hand = (hand.equals("right")) ? "R" : "L";

        String answer = "";
        for (int num : numbers) {
            String Umji = pushNumber(num);
            answer += Umji;

            if(Umji.equals("L")) {leftPos = numpadPos[num]; continue;}
            if(Umji.equals("R")) {rightPos = numpadPos[num]; continue;}
        }
        return answer;
    }

    //num버튼을 누를 때 어디 손을 사용하는가
    private String pushNumber(int num) {
        if(num==1 || num==4 || num==7) return "L";
        if(num==3 || num==6 || num==9) return "R";

        // 2,5,8,0 일때 어디 손가락이 가까운가
        if(getDist(leftPos, num) > getDist(rightPos, num)) return "R";
        if(getDist(leftPos, num) < getDist(rightPos, num)) return "L";

        //같으면 손잡이
        return this.hand;
    }

    //해당 위치와 번호 위치의 거리
    private int getDist(int[] pos, int num) {
        return Math.abs(pos[0]-numpadPos[num][0]) + Math.abs(pos[1]-numpadPos[num][1]);
    }
}

'코딩(알고리즘)' 카테고리의 다른 글

[LEVEL1] 다트 게임  (0) 2022.03.27
[LEVEL1] 비밀지도  (0) 2022.03.26
[LEVEL1] 핸드폰번호 가리기  (0) 2022.03.23
[LEVEL1] 행렬의 덧셈  (0) 2022.03.23
[LEVEL1] 문자열을 정수로 바꾸기  (0) 2022.03.23

+ Recent posts