(프로그래머스) 조이스틱 (Java, 자바)

Archymi·2021년 2월 24일
0

https://programmers.co.kr/learn/courses/30/lessons/42860

문제 설명

조이스틱으로 알파벳 이름을 완성하세요. 맨 처음엔 A로만 이루어져 있습니다.
ex) 완성해야 하는 이름이 세 글자면 AAA, 네 글자면 AAAA

조이스틱을 각 방향으로 움직이면 아래와 같습니다.

▲ - 다음 알파벳
▼ - 이전 알파벳 (A에서 아래쪽으로 이동하면 Z로)
◀ - 커서를 왼쪽으로 이동 (첫 번째 위치에서 왼쪽으로 이동하면 마지막 문자에 커서)
▶ - 커서를 오른쪽으로 이동

예를 들어 아래의 방법으로 JAZ를 만들 수 있습니다.

  • 첫 번째 위치에서 조이스틱을 위로 9번 조작하여 J를 완성합니다.
  • 조이스틱을 왼쪽으로 1번 조작하여 커서를 마지막 문자 위치로 이동시킵니다.
  • 마지막 위치에서 조이스틱을 아래로 1번 조작하여 Z를 완성합니다.
    따라서 11번 이동시켜 "JAZ"를 만들 수 있고, 이때가 최소 이동입니다.

만들고자 하는 이름 name이 매개변수로 주어질 때, 이름에 대해 조이스틱 조작 횟수의 최솟값을 return 하도록 solution 함수를 만드세요.

제한사항

  • name은 알파벳 대문자로만 이루어져 있습니다.
  • name의 길이는 1 이상 20 이하입니다.

입출력 예

namereturn
"JEROEN"56
"JAN"23

코드

import java.util.Arrays;

public class Solution {
    public static final char[] alphabet = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};

    public static int betweenAlphabet(char a, char b) {
        int start = 0;
        int end = 0;
        for (int i = 0; i < alphabet.length; i++) {
            if (alphabet[i] == a)
                start = i;
            if (alphabet[i] == b)
                end = i;
            if (start != 0 && end != 0)
                break;
        }
        return Math.min(Math.abs(a-b), Math.abs(a + 26 - b));
    }

    public static int solution(String name) {
        int answer = 0;
        int index = 0;
        int max = name.length() - 1;
        int min = 0;
        boolean leftChoiceRight = false;
        char[] origin = new char[name.length()];
        char[] right = name.toCharArray();
        
        int count = 0;
        // initialize  origin with "A"
        for (int i = 0; i < origin.length; i++) {
            origin[i] = 'A';
        }

        while(true) {
            int leftChoice = 0;
            int rightChoice = 0;
            char[] copy;

            if(origin[index] != right[index]) {
                answer += betweenAlphabet(origin[index], right[index]);
                origin[index] = right[index];
            }
            if (Arrays.equals(origin, right)) {
                break;
            }
            
            copy = Arrays.copyOf(origin, origin.length);
            // in case rightChoice
            for (int i = index + 1; i <name.length(); i++) {
            	rightChoice += betweenAlphabet(copy[i], right[i]) + 1;
            	copy[i] = right[i];
            	if (Arrays.equals(copy, right)) {
                    break;
                }
            }
            
            copy = Arrays.copyOf(origin, origin.length);
            // in case leftChoice
            for (int i = 1; i <name.length(); i++) {
            	leftChoice += betweenAlphabet(copy[(index + name.length() - i) % name.length()], right[(index + name.length() - i) % name.length()]) + 1;
            	copy[(index + name.length() - i) % name.length()] = right[(index + name.length() - i) % name.length()];
            	if (Arrays.equals(copy, right)) {
                    break;
                }
            }
            if (leftChoiceRight) {
            	return answer + leftChoice;
            }
            
            if (rightChoice <= leftChoice) {
                index = (index + 1) % name.length();
            } else {
            	index = (name.length() + index - 1) % name.length();
            	leftChoiceRight = true;
            }
            answer++;
        }
        return answer;
    }
    
    public static void main(String[] args) {
	System.out.println(solution(new String("JEROEN"))); // 56;
	System.out.println(solution(new String("JAN"))); // 11;
    	System.out.println(solution(new String("BBBAAAB"))); // 9;
    	System.out.println(solution(new String("ABABAAAAABA"))); // 11;
    }
}
profile
겸손하고 꾸준하게.

0개의 댓글