1+ import java .util .*;
12class Solution {
23 public int solution (String name ) {
3- char [] names = name .toCharArray ();
4- int updown = 0 ;
5-
6- // 알파벳 변경 횟수
7- for (char ch : names ) {
8- updown += Math .min (ch - 'A' , 'Z' - ch + 1 );
4+ int upDown = 0 ;
5+ for (char ch : name .toCharArray ()){
6+ upDown += Math .min (ch - 'A' ,'Z' - ch + 1 );
97 }
8+ System .out .println (upDown );
9+
10+ char [] names = name .toCharArray ();
11+ int leftRight = name .length () - 1 ; // 기본값: 오른쪽으로 쭉 직진
12+ // 1. 왼쪽 찍고 오른쪽으로 다시 도착
13+ // 2. 오른쪽 찍고 왼쪽으로 다시 도착
14+ // 3. 왼쪽으로 쭉 직진 , 오른쪽으로 쭉 직진
1015
1116 int len = name .length ();
12- int leftRight = len - 1 ;
13- for (int i = 0 ; i < len ; i ++) {
17+ for (int i = 0 ; i < len ; i ++) {
1418 int next = i + 1 ;
1519
16- // A가 몇개나 있는지 확인
17- while (next < len && names [next ] == 'A' ) {
18- next ++;
20+ while (next < len && names [next ] == 'A' ){
21+ next ++;
1922 }
2023
21- // 1. 오른쪽 찍고 왼쪽으로 방향 전환
22- leftRight = Math . min ( i * 2 + ( len - next ), leftRight ) ;
23-
24- // 2. 왼쪽 찍고 오른쪽으로 방향 전환
25- leftRight = Math .min (( len - next ) * 2 + i , leftRight );
24+ // 1번
25+ int temp1 = ( len - next ) * 2 + i ;
26+ // 2번
27+ int temp2 = ( i * 2 ) + len - next ;
28+ leftRight = Math .min (leftRight , Math . min ( temp1 , temp2 ) );
2629 }
2730
28- return leftRight + updown ;
31+ int answer = leftRight + upDown ;
32+ return answer ;
2933 }
3034}
0 commit comments