11import java .util .*;
22class Solution {
33 public int solution (String name ) {
4- int upDown = 0 ;
5- for (char ch : name .toCharArray ()){
6- upDown += Math .min (ch - 'A' ,'Z' - ch + 1 );
4+ int upDown = 0 ;
5+
6+ for (char ch : name .toCharArray ()) {
7+ upDown += Math .min (ch - 'A' , 'Z' - ch + 1 );
78 }
89 System .out .println (upDown );
910
10- char [] names = name .toCharArray ();
11- int leftRight = name .length () - 1 ; // 기본값: 오른쪽으로 쭉 직진
12- // 1. 왼쪽 찍고 오른쪽으로 다시 도착
13- // 2. 오른쪽 찍고 왼쪽으로 다시 도착
14- // 3. 왼쪽으로 쭉 직진 , 오른쪽으로 쭉 직진
15-
11+ char [] ch = name .toCharArray ();
1612 int len = name .length ();
17- for ( int i = 0 ; i < len ; i ++) {
18- int next = i + 1 ;
19-
20- while (next < len && names [next ] == 'A' ){
21- next ++;
13+ int leftRight = len - 1 ;
14+ for ( int i = 0 ; i < len ; i ++) {
15+ int next = i + 1 ;
16+ while (next < len && ch [next ] == 'A' ){
17+ next ++;
2218 }
19+ // len = 9
20+ // next = 7
21+ // JAAAAAABB
22+
23+ // 왼쪽 찍고, 오른쪽으로
24+ int left = i * 2 + len - next ;
25+ // 오른쪽 돌아서 찍고, 다시 왼쪽으로
26+ int right = (len - next ) * 2 + i ;
2327
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 ));
28+ int min = Math .min (left ,right );
29+ leftRight = Math .min (min ,leftRight );
2930 }
3031
31- int answer = leftRight + upDown ;
32- return answer ;
32+ return upDown + leftRight ;
3333 }
3434}
0 commit comments