[프로그래머스/Greedy] Level 2 조이스틱 (JAVA)

Jiwoo Kim·2021년 3월 26일
0

알고리즘 정복하기

목록 보기
34/85
post-thumbnail
post-custom-banner

문제


풀이


코드

class Solution {

    private static final int A = 65;
    private static final int Z = 90;

    public int solution(String name) {
        return countUpDownMovement(name) + countLeftRightMovement(name);
    }

    private int countUpDownMovement(String str) {
        int count = 0;
        for (char ch : str.toCharArray())
            count += Math.min(ch - A, Z - ch + 1);
        return count;
    }

    private int countLeftRightMovement(String name) {
        int min = name.length() - 1;
        for (int i = 0; i < name.length(); i++) {
            int endOfAIndex = i;
            if (name.charAt(endOfAIndex) == A) {
                while (endOfAIndex < name.length() && name.charAt(endOfAIndex) == A) endOfAIndex++;
                int moveCount = ((i - 1) * 2) + (name.length() - endOfAIndex);
                min = Math.min(min, Math.max(0, moveCount));
            }
        }
        return min;
    }
}

참고

[프로그래머스]조이스틱

post-custom-banner

0개의 댓글