- 난이도: Lv1
프로그래머스 링크: https://school.programmers.co.kr/learn/courses/30/lessons/67256
풀이 링크(GitHub): hayannn/CodingTest_Java/프로그래머스/1/키패드 누르기
풀이 시간 : 12분
class Solution {
public String solution(int[] numbers, String hand) {
StringBuilder answer = new StringBuilder();
int left = 10, right = 12; // * = 10, # = 12
for (int num : numbers) {
if (num == 0) {
num = 11;
}
if (num % 3 == 1) {
left = num;
answer.append("L");
continue;
}
if (num % 3 == 0) {
right = num;
answer.append("R");
continue;
}
int leftDist = getDistance(left, num);
int rightDist = getDistance(right, num);
if (leftDist < rightDist || (leftDist == rightDist && hand.equals("left"))) {
left = num;
answer.append("L");
continue;
}
right = num;
answer.append("R");
}
return answer.toString();
}
public int getDistance(int start, int target) {
return Math.abs(start - target) / 3 + Math.abs(start - target) % 3;
}
}