Skip to content

Commit 0969f33

Browse files
committed
97차 2번 문제풀이
1 parent 92b07c7 commit 0969f33

1 file changed

Lines changed: 16 additions & 12 deletions

File tree

live9/test97/문제2/조진우.js

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,29 @@
11
function 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

0 commit comments

Comments
 (0)