๐Ÿ”Žํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค - ์กฐ์ด์Šคํ‹ฑ

๋ฐ•๋ฏผ์šฐยท2023๋…„ 7์›” 18์ผ
0

๋ฌธ์ œ: 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;
}
profile
๊พธ์ค€ํžˆ, ๊นŠ๊ฒŒ

1๊ฐœ์˜ ๋Œ“๊ธ€

comment-user-thumbnail
2023๋…„ 7์›” 19์ผ

๊ฐ€์น˜ ์žˆ๋Š” ์ •๋ณด ๊ณต์œ ํ•ด์ฃผ์…”์„œ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค.

๋‹ต๊ธ€ ๋‹ฌ๊ธฐ