Skip to content

Commit 88d23bc

Browse files
solution: 모음사전
1 parent b8d055f commit 88d23bc

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

level-2/모음-사전.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//https://github.com/codeisneverodd/programmers-coding-test
2+
//완벽한 정답이 아닙니다.
3+
//정답 1 - codeisneverodd
4+
function solution(word) {
5+
const alphabetRank = { A: 0, E: 1, I: 2, O: 3, U: 4 };
6+
const price = calculatePrice([1], 5);
7+
return word
8+
.split("")
9+
.map((alphabet, index) => 1 + price[index] * alphabetRank[alphabet])
10+
.reduce((acc, curr) => acc + curr, 0);
11+
}
12+
const calculatePrice = (result = [1], targetLength) => {
13+
if (result.length === targetLength) return result;
14+
return calculatePrice([result[0] * 5 + 1, ...result], targetLength);
15+
};
16+
/*
17+
각 자리 문자를 바로 다음 문자로 바꾸는 데에 얼마의 비용이 들까?
18+
4번째 자리 - 1
19+
3번째 자리 - 1*5 + 1 = 6
20+
2번째 자리 - 6*5 + 1 = 31
21+
1번째 자리 - 31*5 + 1 = 156
22+
0번째 자리 - 156*5 + 1 = 781
23+
24+
검증(1부터 시작하므로 1 + 비용)
25+
I => (1 + 781 * 2) = 1563
26+
EIO => (1 + 781 * 1) + (1 + 156 * 2) + (1 + 31 * 3) = 1189
27+
AAAE => 1 + 1 + 1 + (1 + 6 * 1) = 10
28+
AAAAE => 1 + 1 + 1 + 1 + (1 + 1*1) = 6
29+
추천 레퍼런스: https://seongho96.tistory.com/50
30+
*/

0 commit comments

Comments
 (0)