Skip to content

Commit 89ae20b

Browse files
author
Eric
committed
93차 2번 문제풀이
1 parent 9aeb99d commit 89ae20b

1 file changed

Lines changed: 17 additions & 35 deletions

File tree

live9/test93/문제2/황장현.js

Lines changed: 17 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,24 @@
1-
const alphabetValues = {
2-
B: 1,
3-
C: 2,
4-
D: 3,
5-
E: 4,
6-
F: 5,
7-
G: 6,
8-
H: 7,
9-
I: 8,
10-
J: 9,
11-
K: 10,
12-
L: 11,
13-
M: 13,
14-
N: 12,
15-
O: 12,
16-
P: 11,
17-
Q: 10,
18-
R: 9,
19-
S: 8,
20-
T: 7,
21-
U: 6,
22-
V: 5,
23-
W: 4,
24-
X: 3,
25-
Y: 2,
26-
Z: 1,
27-
};
28-
291
function solution(name) {
30-
let answer = 0;
2+
let moveCount = 0;
3+
let minMove = name.length - 1;
4+
315
for (let i = 0; i < name.length; i++) {
32-
if (name[i] === 'A') {
33-
answer += -1;
34-
} else {
35-
answer += alphabetValues[name[i]];
6+
const char = name[i];
7+
moveCount += Math.min(char.charCodeAt(0) - 65, 91 - char.charCodeAt(0));
8+
9+
let nextIdx = i + 1;
10+
while (nextIdx < name.length && name[nextIdx] === 'A') {
11+
nextIdx++;
3612
}
13+
14+
minMove = Math.min(
15+
minMove,
16+
i * 2 + (name.length - nextIdx),
17+
i + 2 * (name.length - nextIdx)
18+
);
3719
}
38-
answer += name.length;
39-
return answer;
20+
21+
return moveCount + minMove;
4022
}
4123

4224
// console.log(solution('JEROEN'));

0 commit comments

Comments
 (0)