코딩테스트연습/프로그래머스

[프로그래머스 - JAVA] 2020 카카오 인턴십키패드 누르기

:)jun 2021. 10. 24. 14:41

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

class Solution {
    public String solution(int[] numbers, String hand) {
        
        int k=1;
		int la=3, lb=0, ra=3, rb=2;
		int a, b;
		StringBuffer sb = new StringBuffer();
		
		int[][] dial = new int[4][3];
		for(int i=0;i<4;i++) {
			for(int j=0; j<dial[i].length; j++) {
				dial[i][j]=k;
				k++;
			}
		}
		dial[3][0]=77;
		dial[3][1]=0;
		dial[3][2]=99;
		
		for (int number : numbers) {
			if (number == 1 || number == 4 || number == 7 ) {
				for(int i=0; i<4; i++) {
					for(int j=0; j<3; j++) {
						if(dial[i][j]== number) {
							la=i; lb=j;
							sb.append("L");
						}
					}
				}
			}
			else if(number == 3 || number == 6 || number == 9) {
				for(int i=0; i<4; i++) {
					for(int j=0; j<3; j++) {
						if(dial[i][j]== number) {
							ra=i; rb=j;
							sb.append("R");
						}
					}
				}
			}
			else {
				for(int i=0; i<4; i++) {
					for(int j=0; j<3; j++) {
						if(dial[i][j]== number) {
							a=i; b=j;
							int BetweenL = Math.abs(a-la)+Math.abs(b-lb);
							int BetweenR = Math.abs(a-ra)+Math.abs(b-rb);
							if (BetweenL < BetweenR) {
								la=i; lb=j;
								sb.append("L");
							}
							else if(BetweenL > BetweenR) {
								ra=i; rb=j;
								sb.append("R");
							}
							else {
								if(hand.equals("right")) {
									ra=i; rb=j;
									sb.append("R");
								}
								else {
									la=i; lb=j;
									sb.append("L");
								}
							}
						}
					}
				}
			}
		}
		
        String answer = sb.toString();
        return answer;
    }
}