File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11function solution ( name ) {
22 let nameArr = name . split ( "" ) ;
3- let currentValue = "A" ;
43 let totalCount = 0 ;
54
5+ // 1. 위아래 조작: 각 문자에 대해 계산
66 for ( let i = 0 ; i < nameArr . length ; i ++ ) {
7- let upDownCount = 0 ;
8- let leftRightCount = 0 ;
7+ const charCode = nameArr [ i ] . charCodeAt ( 0 ) ;
8+ const diff = Math . min ( charCode - 65 , 91 - charCode ) ;
9+ totalCount += diff ;
10+ }
911
10- upDownCount = Number (
11- Math . min (
12- Math . abs ( nameArr [ i ] . charCodeAt ( 0 ) - currentValue . charCodeAt ( 0 ) ) ,
13- 26 - Math . abs ( nameArr [ i ] . charCodeAt ( 0 ) - currentValue . charCodeAt ( 0 ) )
14- )
15- ) ;
12+ // 2. 좌우 조작: 커서를 어떻게 이동하면 최소인지 계산
13+ let minMove = name . length - 1 ;
1614
17- leftRightCount = i ;
15+ for ( let i = 0 ; i < name . length ; i ++ ) {
16+ let next = i + 1 ;
17+ while ( next < name . length && name [ next ] === "A" ) {
18+ next ++ ;
19+ }
1820
19- totalCount += Math . max ( upDownCount , leftRightCount ) ;
20- currentValue = nameArr [ i ] ;
21+ // 좌측으로 갔다가 우측으로 가는 경우 고려
22+ const move = i + name . length - next + Math . min ( i , name . length - next ) ;
23+ minMove = Math . min ( minMove , move ) ;
2124 }
2225
26+ totalCount += minMove ;
2327 return totalCount ;
2428}
2529
You can’t perform that action at this time.
0 commit comments