From 3684246080312f04fdf83c08b50ca10065256310 Mon Sep 17 00:00:00 2001 From: Jiwon Park Date: Sat, 30 Nov 2024 18:14:40 +0900 Subject: [PATCH] =?UTF-8?q?Add=20#6=20=EC=A1=B0=EC=9D=B4=EC=8A=A4=ED=8B=B1?= =?UTF-8?q?=20-=20=EB=B0=95=EC=A7=80=EC=9B=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\353\260\225\354\247\200\354\233\220.java" | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 "greedy/\354\241\260\354\235\264\354\212\244\355\213\261/\353\260\225\354\247\200\354\233\220.java" diff --git "a/greedy/\354\241\260\354\235\264\354\212\244\355\213\261/\353\260\225\354\247\200\354\233\220.java" "b/greedy/\354\241\260\354\235\264\354\212\244\355\213\261/\353\260\225\354\247\200\354\233\220.java" new file mode 100644 index 0000000..550aead --- /dev/null +++ "b/greedy/\354\241\260\354\235\264\354\212\244\355\213\261/\353\260\225\354\247\200\354\233\220.java" @@ -0,0 +1,26 @@ +class Solution { + public int solution(String name) { + int answer = 0; + int length = name.length(); + int move = length - 1; + + for (int i = 0; i < length; i++) { + // 알파벳 변경 비용 + char ch = name.charAt(i); + int up = ch - 'A'; + int down = 'Z' - ch + 1; + answer += Math.min(up, down); + + // 이동 비용 + int next = i + 1; + while (next < length && name.charAt(next) == 'A') { + next++; + } + move = Math.min(move, i + length - next + Math.min(i, length - next)); + } + + answer += move; + return answer; + } +} +