Skip to content

[17주차] 풀이 추가 - codeisneverodd #68

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions level-2/모음-사전.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//https://github.com/codeisneverodd/programmers-coding-test
//완벽한 정답이 아닙니다.
//정답 1 - codeisneverodd
function solution(word) {
const alphabetRank = { A: 0, E: 1, I: 2, O: 3, U: 4 };
const price = calculatePrice([1], 5);
return word
.split("")
.map((alphabet, index) => 1 + price[index] * alphabetRank[alphabet])
.reduce((acc, curr) => acc + curr, 0);
}
const calculatePrice = (result = [1], targetLength) => {
if (result.length === targetLength) return result;
return calculatePrice([result[0] * 5 + 1, ...result], targetLength);
};
/*
각 자리 문자를 바로 다음 문자로 바꾸는 데에 얼마의 비용이 들까?
4번째 자리 - 1
3번째 자리 - 1*5 + 1 = 6
2번째 자리 - 6*5 + 1 = 31
1번째 자리 - 31*5 + 1 = 156
0번째 자리 - 156*5 + 1 = 781

검증(1부터 시작하므로 1 + 비용)
I => (1 + 781 * 2) = 1563
EIO => (1 + 781 * 1) + (1 + 156 * 2) + (1 + 31 * 3) = 1189
AAAE => 1 + 1 + 1 + (1 + 6 * 1) = 10
AAAAE => 1 + 1 + 1 + 1 + (1 + 1*1) = 6
추천 레퍼런스: https://seongho96.tistory.com/50
*/
28 changes: 28 additions & 0 deletions level-2/하노이의-탑.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//https://github.com/codeisneverodd/programmers-coding-test
//완벽한 정답이 아닙니다.
//정답 1 - codeisneverodd
function solution(n) {
return move(1, 3, 2, n);
}
const move = (departure, destination, waypoint, numberOfPlate) => {
if (numberOfPlate === 1) return [[departure, destination]];
return [
...move(departure, waypoint, destination, numberOfPlate - 1),
...move(departure, destination, waypoint, 1),
...move(waypoint, destination, departure, numberOfPlate - 1),
];
};
//재귀를 생각해보기에 좋은 문제입니다.
//추천 레퍼런스 https://shoark7.github.io/programming/algorithm/tower-of-hanoi
/*
n개가 있다면
1. 1->2로 n-1개를 옮김
2. 1->3으로 가장 큰 1개를 옮김
3. 2->1로 n-2개를 옮김
4. 2->3으로 2번에 있는 것 중 가장 큰 1개를 옮김
의 반복

결국 무엇이든 a -> b 로 n 를 옮기는 동작의 반복이므로 이를 재귀로 표현하면 됨.
a->b 로 n 을 옮기는 것은 a->c로 n-1개를 옮겨놓고, a->b로 하나를 옮긴 후, c->b로 n-1개를 옮기는 것의 반복
함수에서 a는 depature(출발지), b는 destination(도착지), c는 waypoint(경유지)로 작성되어있음.
*/