Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit cd569a0

Browse files
Merge branch 'main' into v0.4.0
2 parents 008a3ca + 5e9192e commit cd569a0

12 files changed

+383
-17
lines changed

‎README.md

Lines changed: 98 additions & 9 deletions
Large diffs are not rendered by default.

‎level-2/2-x-n-타일링.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//https://github.com/codeisneverodd/programmers-coding-test
2+
//완벽한 정답이 아닙니다.
3+
//정답 1 - jaewon1676
4+
function solution(n) {
5+
let dp = [0, 1, 2] // n이 1, 2일때는 바로 답을 출력,
6+
if (n>2){ // n이 3 이상이면 필요한 만큼의 수 까지만 수를 만들어준다.
7+
for (let i=3; i<=n; i++){
8+
dp.push((dp[i-1] + dp[i-2]) % 1000000007);
9+
}
10+
}
11+
return dp[n]
12+
}
13+
/*
14+
n이 1일땐 1, 2일땐 2, 3일땐 3, 4일땐 5 . . 의 식이 보인다.
15+
n = (n - 1) + (n - 2)의 식으로 구할 수 있고,
16+
제한 사항을 주의해서 풀어보자. */

‎level-2/3-x-n-타일링.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//https://github.com/codeisneverodd/programmers-coding-test
2+
//더 좋은 풀이가 존재할 수 있습니다.
3+
//정답 1 - codeisneverodd
4+
function solution(n) {
5+
if (n % 2 !== 0) return 0;
6+
7+
const getCount = n => {
8+
const k = n / 2;
9+
const count = [3, 11, ...Array(k - 2)];
10+
const divider = 1000000007;
11+
for (let i = 2; i < k; i++) {
12+
count[i] = (4 * count[i - 1] - count[i - 2] + divider) % divider;
13+
}
14+
return count[count.length - 1];
15+
};
16+
17+
return getCount(n);
18+
}

‎level-2/N-Queen.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//https://github.com/codeisneverodd/programmers-coding-test
2+
//완벽한 정답이 아닙니다.
3+
//정답 1 - codeisneverodd
4+
function solution(n) {
5+
/*
6+
1. 0번째 행에 0번째 queen을 놓는다.
7+
2. 그 다음 행의 퀸은 이전 퀸들의 범위와 겹치지 않는 곳에 놓는다. 퀸은 한 행에 반드시 하나 두어야한다.
8+
3. 마지막 열까지 도달하면 성공으로 간주하고 answer에 1을 더한다.
9+
4. 0번째 queen의 위치를 바꿔가며 모두 시도한다.
10+
4. 단, 체스판은 일차원 배열로 선언하고 index = 행, 값 = 열 로 생각한다.
11+
*/
12+
let answer = 0;
13+
const canBePlacedOn = (chess, currentRow) => {
14+
//해당 행에 둔 queen이 유효한지
15+
for (let prevRow = 0; prevRow < currentRow; prevRow++) {
16+
const onDiagonal = currentRow - prevRow === Math.abs(chess[currentRow] - chess[prevRow])
17+
const onStraight = chess[prevRow] === chess[currentRow]
18+
if (onDiagonal || onStraight) return false
19+
}
20+
return true
21+
}
22+
const placeQueen = (chess, currentRow) => {
23+
//queen을 배치하다가 끝 행에 도착하면 1을 리턴, 도착하지 못하면 0을 리턴하여, 재귀적으로 모든 경우를 합하여 리턴
24+
let count = 0
25+
if (currentRow === chess.length) return 1
26+
for (let currentQueen = 0; currentQueen < n; currentQueen++) {
27+
//queen을 우선 배치한 후 가능한지 살펴본다.
28+
chess[currentRow] = currentQueen
29+
if (canBePlacedOn(chess, currentRow)) count += placeQueen(chess, currentRow + 1)
30+
}
31+
return count
32+
}
33+
for (let firstQueen = 0; firstQueen < n; firstQueen++) {
34+
const chess = new Array(n).fill(-1)
35+
chess[0] = firstQueen
36+
answer += placeQueen(chess, 1)
37+
}
38+
return answer;
39+
}

‎level-2/교점에-별-만들기.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//https://github.com/codeisneverodd/programmers-coding-test
2+
//더 좋은 풀이가 존재할 수 있습니다.
3+
//정답 1 - codeisneverodd
4+
function solution(line) {
5+
const getCrossPoint = ([A, B, E], [C, D, F]) => {
6+
if (A * D - B * C === 0) return [Infinity, Infinity];
7+
return [(B * F - E * D) / (A * D - B * C), (E * C - A * F) / (A * D - B * C)];
8+
}; //문제 설명 최하단 참조
9+
10+
const crossPoints = line.flatMap((lineA, i) =>
11+
line
12+
.slice(i + 1)
13+
.map(lineB => getCrossPoint(lineA, lineB))
14+
.filter(([x, y]) => Number.isInteger(x) && Number.isInteger(y))
15+
);
16+
17+
const generateCanvas = crossPoints => {
18+
const xPoints = [...crossPoints.map(([x, y]) => x)];
19+
const yPoints = [...crossPoints.map(([x, y]) => y)];
20+
const [minX, maxX] = [Math.min(...xPoints), Math.max(...xPoints)];
21+
const [minY, maxY] = [Math.min(...yPoints), Math.max(...yPoints)];
22+
const xLength = Math.abs(maxX - minX) + 1;
23+
const yLength = Math.abs(maxY - minY) + 1;
24+
25+
return {
26+
canvas: Array.from({ length: yLength }, () => Array(xLength).fill('.')),
27+
draw([x, y], value) {
28+
this.canvas[Math.abs(y - maxY)][Math.abs(x - minX)] = value;
29+
},
30+
print() {
31+
return this.canvas.map(row => row.join(''));
32+
},
33+
};
34+
};
35+
36+
const canvas = generateCanvas(crossPoints);
37+
38+
crossPoints.forEach(point => {
39+
canvas.draw(point, '*');
40+
});
41+
42+
return canvas.print();
43+
}

‎level-2/멀리-뛰기.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//https://github.com/codeisneverodd/programmers-coding-test
2+
//더 좋은 풀이가 존재할 수 있습니다.
3+
//정답 1 - codeisneverodd
4+
function solution(n) {
5+
if (n < 2) return 1;
6+
const count = [0, 1, 2, ...Array(n - 2).fill(0)];
7+
count.forEach((_, i) => {
8+
if (i > 2) count[i] = (count[i - 2] + count[i - 1]) % 1234567;
9+
});
10+
return count[n];
11+
}
12+
//재귀를 사용하면 콜스택 오버플로우가 발생합니다.

‎level-2/방문-길이.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//https://github.com/codeisneverodd/programmers-coding-test
2+
//더 좋은 풀이가 존재할 수 있습니다.
3+
//정답 1 - codeisneverodd
4+
function solution(dirs) {
5+
const move = ([x, y], dir) => {
6+
let next = [x, y];
7+
if (dir === 'U') next = [x, y + 1];
8+
if (dir === 'D') next = [x, y - 1];
9+
if (dir === 'R') next = [x + 1, y];
10+
if (dir === 'L') next = [x - 1, y];
11+
if (Math.abs(next[0]) > 5 || Math.abs(next[1]) > 5) return [x, y];
12+
return next;
13+
};
14+
15+
const isSameRoute = ([s1, e1], [s2, e2]) => {
16+
const isSamePoint = ([x1, y1], [x2, y2]) => x1 === x2 && y1 === y2;
17+
return (isSamePoint(s1, s2) && isSamePoint(e1, e2)) || (isSamePoint(s1, e2) && isSamePoint(s2, e1));
18+
};
19+
20+
const trace = {
21+
visited: [],
22+
visit(start, end) {
23+
if (start[0] === end[0] && start[1] === end[1]) return;
24+
if (!this.visited.find(route => isSameRoute(route, [start, end]))) this.visited.push([start, end]);
25+
},
26+
};
27+
28+
let current = [0, 0];
29+
30+
dirs.split('').forEach(dir => {
31+
const next = move(current, dir);
32+
trace.visit(current, next);
33+
current = next;
34+
});
35+
36+
return trace.visited.length;
37+
}

‎level-2/이진-변환-반복하기.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//https://github.com/codeisneverodd/programmers-coding-test
2+
//더 좋은 풀이가 존재할 수 있습니다.
3+
//정답 1 - codeisneverodd
4+
function solution(s) {
5+
const removeZero = s => {
6+
const removed = s
7+
.split('')
8+
.filter(n => n !== '0')
9+
.join('');
10+
return { removed, count: s.length - removed.length };
11+
};
12+
13+
const convertToBinary = (s, turnCount, removedCount) => {
14+
if (s === '1') return [turnCount, removedCount];
15+
const { removed, count } = removeZero(s);
16+
return convertToBinary(removed.length.toString(2), turnCount + 1, removedCount + count);
17+
};
18+
19+
return convertToBinary(s, 0, 0);
20+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//https://github.com/codeisneverodd/programmers-coding-test
2+
//더 좋은 풀이가 존재할 수 있습니다.
3+
//정답 1 - codeisneverodd
4+
function solution(n, wires) {
5+
const hasOneOfWire = (tree, [a, b]) => tree.includes(a) || tree.includes(b);
6+
7+
const convertWiresToTree = wires => [...new Set(wires.flat())];
8+
9+
const generateTree = (wires, tree) => {
10+
if (!wires.find(wire => hasOneOfWire(tree, wire))) return tree;
11+
12+
const nextWires = wires.filter(wire => !hasOneOfWire(tree, wire));
13+
const nextTree = [...tree, ...convertWiresToTree(wires.filter(wire => hasOneOfWire(tree, wire)))];
14+
15+
return [...new Set(generateTree(nextWires, nextTree))];
16+
};
17+
18+
let minDiff = Infinity;
19+
const length = convertWiresToTree(wires).length;
20+
21+
wires.forEach((_, i) => {
22+
const [initWire, ...remainWires] = wires.filter((_, j) => j !== i);
23+
const lengthA = generateTree(remainWires, convertWiresToTree([initWire])).length;
24+
const diff = Math.abs(lengthA - (length - lengthA));
25+
minDiff = Math.min(diff, minDiff);
26+
});
27+
28+
return minDiff;
29+
}

‎level-2/줄-서는-방법.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//https://github.com/codeisneverodd/programmers-coding-test
2+
//더 좋은 풀이가 존재할 수 있습니다.
3+
//정답 1 - codeisneverodd
4+
function solution(n, k) {
5+
const getFactorial = n => {
6+
const result = [1, 1, 2, ...Array(n - 2)];
7+
result.forEach((_, i) => {
8+
if (i > 2) result[i] = result[i - 1] * i;
9+
});
10+
return result;
11+
};
12+
13+
const getDivision = (dividend, divisor) => {
14+
const quotient = Math.floor(dividend / divisor);
15+
const remainder = dividend % divisor;
16+
return [quotient, remainder];
17+
};
18+
19+
const stepCount = getFactorial(n).reverse();
20+
21+
const generateSteps = (k, step) => {
22+
const [q, r] = getDivision(k, stepCount[step]);
23+
if (r === 0) return [q];
24+
return [q, ...generateSteps(r, step + 1)];
25+
};
26+
27+
const answer = [];
28+
29+
const steps = generateSteps(k - 1, 0);
30+
31+
const notUsedNums = Array.from({ length: n }, (_, i) => i + 1);
32+
33+
steps.slice(1).forEach(q => {
34+
answer.push(notUsedNums[q]);
35+
notUsedNums.splice(q, 1);
36+
});
37+
38+
return [...answer, ...notUsedNums];
39+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /