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 7602035

Browse files
Merge pull request codeisneverodd#98 from codeisneverodd/codeisneverodd
feat: 새로운 해설 4개 추가
2 parents 939f74d + 9bb46e0 commit 7602035

File tree

5 files changed

+149
-1
lines changed

5 files changed

+149
-1
lines changed

‎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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//https://github.com/codeisneverodd/programmers-coding-test
22
//더 좋은 풀이가 존재할 수 있습니다.
3-
//정답 1 - codeisneveordd
3+
//정답 1 - codeisneverodd
44
function solution(n) {
55
if (n < 2) return 1;
66
const count = [0, 1, 2, ...Array(n - 2).fill(0)];

‎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+
}
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 によって変換されたページ (->オリジナル) /