๋ฌธ์ : https://school.programmers.co.kr/learn/courses/30/lessons/42860
์นดํ ๊ณ ๋ฆฌ: ๊ทธ๋ฆฌ๋
์ถ์ฒ: ํ๋ก๊ทธ๋๋จธ์ค ์ฝ๋ฉ ํ ์คํธ ๊ณ ๋์ Kit, https://school.programmers.co.kr/learn/challenges?tab=algorithm_practice_kit
const minMove = (alphabet) => {
return Math.min(alphabet.charCodeAt() - 65, 91 - alphabet.charCodeAt());
}
function solution(name) {
let answer = 0;
let minMoveCnt = name.length - 1;
[...name].map((n, i) => {
answer += minMove(n);
let idx = i + 1;
while (idx < name.length && name[idx] === 'A') {
idx++;
}
minMoveCnt = Math.min(
minMoveCnt,
i * 2 + name.length - idx,
i + 2 * (name.length - idx),
);
});
return answer + minMoveCnt;
}
๊ฐ์น ์๋ ์ ๋ณด ๊ณต์ ํด์ฃผ์ ์ ๊ฐ์ฌํฉ๋๋ค.