💻 문제 출처 : 프로그래머스_키패드 누르기
import java.util.*;
class Solution {
public String solution(int[] numbers, String hand) {
int[][] numberPosition = new int[12][2];
for(int i = 0; i < 10; i++) {
if (i == 0) {
numberPosition[0] = new int[]{3, 1};
numberPosition[10] = new int[]{3, 0};
numberPosition[11] = new int[]{3, 2};
} else {
numberPosition[i] = new int[]{(i - 1) / 3, (i - 1) % 3};
}
}
StringBuilder sb = new StringBuilder();
int leftThumb = 10;
int rightThumb = 11;
for(int i : numbers) {
if (i == 1 || i == 4 || i == 7) {
sb.append("L");
leftThumb = i;
} else if (i == 3 || i == 6 || i == 9) {
sb.append("R");
rightThumb = i;
} else {
int leftDistance = Math.abs(numberPosition[i][0] - numberPosition[leftThumb][0]) + Math.abs(numberPosition[i][1] - numberPosition[leftThumb][1]);
int rightDistance = Math.abs(numberPosition[i][0] - numberPosition[rightThumb][0]) + Math.abs(numberPosition[i][1] - numberPosition[rightThumb][1]);
if(leftDistance < rightDistance) {
sb.append("L");
leftThumb = i;
} else if (rightDistance < leftDistance) {
sb.append("R");
rightThumb = i;
} else {
if(hand.equals("right")) {
sb.append("R");
rightThumb = i;
} else {
sb.append("L");
leftThumb = i;
}
}
}
}
return sb.toString();
}
}
📌 문제 풀이 설명
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]);
}
}
📌 문제 풀이 설명
class Solution {
int tempL = 10;
int tempR = 12;
String myhand;
public String solution(int[] numbers, String hand) {
myhand = ((hand.equals("right"))? "R": "L");
String answer = "";
for(int i=0 ; i< numbers.length ; i++) {
switch(numbers[i]) {
case 1: case 4: case 7:
answer += "L";
tempL = numbers[i];
break;
case 3: case 6: case 9:
answer += "R";
tempR = numbers[i];
break;
default:
String tempHand = checkHand(numbers[i]);
if(tempHand.equals("R"))
tempR = numbers[i] + ((numbers[i] == 0)? 11:0);
else tempL = numbers[i] + ((numbers[i] == 0)? 11:0);
answer += tempHand;
break;
}
}
return answer;
}
private String checkHand(int tempNum) {
int leftDistance = 0;
int rightDistance = 0;
if(tempNum == 0) tempNum = 11;
leftDistance = Math.abs((tempNum-1)/3 - (tempL-1)/3) + Math.abs((tempNum-1)%3 - (tempL-1)%3);
rightDistance = Math.abs((tempNum-1)/3 - (tempR-1)/3) + Math.abs((tempNum-1)%3 - (tempR-1)%3);
System.out.println(tempNum + ": " + leftDistance + ", " + rightDistance);
return ((leftDistance == rightDistance)? myhand: (leftDistance > rightDistance)? "R": "L");
}
}
📌 문제 풀이 설명