diff --git "a/level-2/\353\252\250\354\235\214-\354\202\254\354\240\204.js" "b/level-2/\353\252\250\354\235\214-\354\202\254\354\240\204.js" new file mode 100644 index 0000000..e1df7ce --- /dev/null +++ "b/level-2/\353\252\250\354\235\214-\354\202\254\354\240\204.js" @@ -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 +*/ diff --git "a/level-2/\355\225\230\353\205\270\354\235\264\354\235\230-\355\203\221.js" "b/level-2/\355\225\230\353\205\270\354\235\264\354\235\230-\355\203\221.js" new file mode 100644 index 0000000..5b57c2d --- /dev/null +++ "b/level-2/\355\225\230\353\205\270\354\235\264\354\235\230-\355\203\221.js" @@ -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(경유지)로 작성되어있음. +*/