티스토리 뷰

문제출처: 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

풀이법: 그냥 생각대로 풀었다

Comment: 

1) 이클립스에서 이 코드는 문제없지만  프로그래머스에선 테스트 케이스 통과 못함 (hands가 매개변수라서)

2) String에서 equals 랑 == 연산자 차이

import java.util.ArrayList;

public class Solution {

	public static ArrayList<String> answer = new ArrayList();

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//String hand = "right";
		String hand = "left";
		//int[] numbers = {1, 3, 4, 5, 8, 2, 1, 4, 5, 9, 5};	
		int[] numbers = {7, 0, 8, 2, 8, 3, 1, 5, 7, 6, 2};
		//int[] numbers = {1,2,3,4,5,6,7,8,9,0};
		
		// default
		int[][] keypad = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 }, { 11, 0, 12 } };
		int[] lpos = { 3, 0 };
		int[] rpos = { 3, 2 };
		
		ArrayList<Integer> left = new ArrayList();
		ArrayList<Integer> right = new ArrayList();
		ArrayList<Integer> center = new ArrayList();
		left.add(1);
		left.add(4);
		left.add(7);
		right.add(3);
		right.add(6);
		right.add(9);
		center.add(2);
		center.add(5);
		center.add(8);
		center.add(0);

		for (int i = 0; i < numbers.length; i++) {
			int next = numbers[i];
			if (left.contains((int) next)) { // left
				if (next == 1) {
					lpos[0] = 0;
					lpos[1] = 0;
				} else if (next == 4) {
					lpos[0] = 1;
					lpos[1] = 0;
				} else if (next == 7) {
					lpos[0] = 2;
					lpos[1] = 0;
				}
				answer.add("L");

			} else if (right.contains((int) next)) { // right
				if (next == 3) {
					rpos[0] = 0;
					rpos[1] = 2;
				} else if (next == 6) {
					rpos[0] = 1;
					rpos[1] = 2;
				} else if (next == 9) {
					rpos[0] = 2;
					rpos[1] = 2;
				}
				answer.add("R");
			} else if (center.contains((int) next)) { // center

				int rdistance = 0;
				int ldistance = 0;

				if (next == 2) { // 01
					rdistance = Math.abs(rpos[0] - 0) + Math.abs(rpos[1] - 1);
					ldistance = Math.abs(lpos[0] - 0) + Math.abs(lpos[1] - 1);

					if (rdistance > ldistance) { // lpos is closer
						lpos[0] = 0;
						lpos[1] = 1;
						answer.add("L");

					} else if (rdistance < ldistance) { // rpos is closer
						rpos[0] = 0;
						rpos[1] = 1;
						answer.add("R");

					} else if (rdistance == ldistance) { // same distance
						if (hand == "left") {// left handed
							lpos[0] = 0;
							lpos[1] = 1;
							answer.add("L");

						} else if (hand == "right") {// right handed
							rpos[0] = 0;
							rpos[1] = 1;
							answer.add("R");
						}

					}

				} else if (next == 5) { // 11
					rdistance = Math.abs(rpos[0] - 1) + Math.abs(rpos[1] - 1);
					ldistance = Math.abs(lpos[0] - 1) + Math.abs(lpos[1] - 1);

					if (rdistance > ldistance) { // lpos is closer
						lpos[0] = 1;
						lpos[1] = 1;
						answer.add("L");

					} else if (rdistance < ldistance) { // rpos is closer
						rpos[0] = 1;
						rpos[1] = 1;
						answer.add("R");

					} else if (rdistance == ldistance) { // same distance
						if (hand == "left") {// left handed
							lpos[0] = 1;
							lpos[1] = 1;
							answer.add("L");

						} else if (hand == "right") {// right handed
							rpos[0] = 1;
							rpos[1] = 1;
							answer.add("R");
						}

					}

				} else if (next == 8) { // 21
					rdistance = Math.abs(rpos[0] - 2) + Math.abs(rpos[1] - 1);
					ldistance = Math.abs(lpos[0] - 2) + Math.abs(lpos[1] - 1);

					if (rdistance > ldistance) { // lpos is closer
						lpos[0] = 2;
						lpos[1] = 1;
						answer.add("L");

					} else if (rdistance < ldistance) { // rpos is closer
						rpos[0] = 2;
						rpos[1] = 1;
						answer.add("R");

					} else if (rdistance == ldistance) { // same distance
						if (hand == "left") {// left handed
							lpos[0] = 2;
							lpos[1] = 1;
							answer.add("L");

						} else if (hand == "right") {// right handed
							rpos[0] = 2;
							rpos[1] = 1;
							answer.add("R");
						}

					}

				} else if (next == 0) { // 31
					rdistance = Math.abs(rpos[0] - 3) + Math.abs(rpos[1] - 1);
					ldistance = Math.abs(lpos[0] - 3) + Math.abs(lpos[1] - 1);

					if (rdistance > ldistance) { // lpos is closer
						lpos[0] = 3;
						lpos[1] = 1;
						answer.add("L");

					} else if (rdistance < ldistance) { // rpos is closer
						rpos[0] = 3;
						rpos[1] = 1;
						answer.add("R");

					} else if (rdistance == ldistance) { // same distance
						if (hand == "left") {// left handed
							lpos[0] = 3;
							lpos[1] = 1;
							answer.add("L");

						} else if (hand == "right") {// right handed
							rpos[0] = 3;
							rpos[1] = 1;
							answer.add("R");
						}
					}
				}
			}
		}
		
		for(int i = 0; i < answer.size();i++) {
			System.out.print(answer.get(i));
		}
		System.out.println();
		StringBuilder sb = new StringBuilder();
		for(int i = 0; i < answer.size();i++) {
			sb.append(answer.get(i));
		}
		System.out.println(sb.toString());

	}

}