Skip to content

Commit 619b9ab

Browse files
author
gyeong-hyeon-kim
committed
Solved 1 problem.
1 parent 908ba02 commit 619b9ab

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

level-2/조이스틱.js

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//https://github.com/codeisneverodd/programmers-coding-test
2+
//완벽한 정답이 아닙니다.
3+
function solution(name) {
4+
var answer = 0;
5+
const length = name.length;
6+
let upDownCount = 0
7+
let leftRightCountList = [length - 1]//한 방향으로 쭉 갔을 때
8+
for (let i = 0; i < length; i++) upDownCount += minUpOrDownCount(name[i])
9+
for (let startOfA = 0; startOfA < name.length; startOfA++) {
10+
let endOfA = startOfA + 1;
11+
while (endOfA < length && name[endOfA] === 'A') endOfA++;
12+
const [moveToStartOfA, moveToEndOfA] = [startOfA, length - endOfA]
13+
leftRightCountList.push(moveToStartOfA * 2 + moveToEndOfA)// 0 -> A.., 0 <- A.., ..A <- -1
14+
leftRightCountList.push(moveToEndOfA * 2 + moveToStartOfA)//시작부터 뒤로 가는 경우 ..A <- -1, ..A -> -1, 0 -> A..
15+
}
16+
answer = upDownCount + Math.min(...leftRightCountList)
17+
return answer;
18+
}
19+
20+
function minUpOrDownCount(destination) {
21+
const Alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
22+
const index = Alphabet.indexOf(destination)
23+
return Math.min(index, Alphabet.length - index)
24+
}

0 commit comments

Comments
 (0)