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
import java.util.HashMap;
class Solution {
public String solution(int[] numbers, String hand) {
String answer = "";
HashMap<Integer, int[]> keypadPositions = new HashMap<>();
keypadPositions.put(1, new int[]{0, 0});
keypadPositions.put(2, new int[]{0, 1});
keypadPositions.put(3, new int[]{0, 2});
keypadPositions.put(4, new int[]{1, 0});
keypadPositions.put(5, new int[]{1, 1});
keypadPositions.put(6, new int[]{1, 2});
keypadPositions.put(7, new int[]{2, 0});
keypadPositions.put(8, new int[]{2, 1});
keypadPositions.put(9, new int[]{2, 2});
keypadPositions.put(0, new int[]{3, 1});
int[] leftHandPosition = {3, 0};
int[] rightHandPosition = {3, 2};
for (int num : numbers){
if (num == 1 || num == 4 || num == 7){
answer += 'L';
leftHandPosition = keypadPositions.get(num);
}else if (num == 3 || num == 6 || num == 9){
answer += 'R';
rightHandPosition = keypadPositions.get(num);
}else{
int leftHandDiff = getDiff(leftHandPosition, num, keypadPositions);
int rightHandDiff = getDiff(rightHandPosition, num, keypadPositions);
if (leftHandDiff == rightHandDiff) {
if (hand.equals("right")) {
answer += 'R';
rightHandPosition = keypadPositions.get(num);
} else {
answer += 'L';
leftHandPosition = keypadPositions.get(num);
}
}else if (leftHandDiff < rightHandDiff){
answer += 'L';
leftHandPosition = keypadPositions.get(num);
}else{
answer += 'R';
rightHandPosition = keypadPositions.get(num);
}
}
}
return answer;
}
private int getDiff(int[] currentHandPosition, int num, HashMap<Integer, int[]> keypadPositions) {
int[] nextNumPosition = keypadPositions.get(num);
int xDiff = Math.abs(currentHandPosition[1] - nextNumPosition[1]);
int yDiff = Math.abs(currentHandPosition[0] - nextNumPosition[0]);
return xDiff + yDiff;
}
}
'Problem-solving > 프로그래머스' 카테고리의 다른 글
프로그래머스 - (2019 KAKAO BLIND RECRUITMENT) 실패율 (Java) (0) | 2020.11.22 |
---|---|
프로그래머스 - (2018 KAKAO BLIND RECRUITMENT) [1차]비밀지도 (Java) (0) | 2020.11.21 |
프로그래머스 - (2019 카카오 개발자 겨울 인턴십) 크레인 인형뽑기 게임 (Java) (0) | 2020.11.21 |
프로그래머스 - (2018 KAKAO BLIND RECRUITMENT) [3차]자동완성 (Python3) (0) | 2020.09.03 |
프로그래머스 - (2019 KAKAO BLIND RECRUITMENT) 블록 게임 (Python3) (0) | 2020.09.02 |
댓글