Skip to content

Commit cd569a0

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

12 files changed

+383
-17
lines changed

Diff for: README.md

+98-9
Large diffs are not rendered by default.

Diff for: level-2/2-x-n-타일링.js

+16
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+
제한 사항을 주의해서 풀어보자. */

Diff for: level-2/3-x-n-타일링.js

+18
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+
}

Diff for: level-2/N-Queen.js

+39
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+
}

Diff for: level-2/교점에-별-만들기.js

+43
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+
}

Diff for: level-2/멀리-뛰기.js

+12
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+
//재귀를 사용하면 콜스택 오버플로우가 발생합니다.

Diff for: level-2/방문-길이.js

+37
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+
}

Diff for: level-2/이진-변환-반복하기.js

+20
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+
}

Diff for: level-2/전력망을-둘로-나누기.js

+29
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+
}

Diff for: level-2/줄-서는-방법.js

+39
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+
}

Diff for: level-2/쿼드압축-후-개수-세기.js

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//https://github.com/codeisneverodd/programmers-coding-test
2+
//더 좋은 풀이가 존재할 수 있습니다.
3+
//정답 1 - codeisneverodd
4+
function solution(arr) {
5+
const quad = matrix => {
6+
const length = matrix.length;
7+
const half = length / 2;
8+
const pass = matrix => matrix.every(row => row.every(v => v === matrix[0][0]));
9+
10+
if (pass(matrix)) return [matrix[0][0]];
11+
if (length <= 2) return matrix;
12+
13+
const startPoints = [
14+
[0, 0],
15+
[0, half],
16+
[half, 0],
17+
[half, half],
18+
];
19+
20+
return startPoints.map(([r, c]) => quad(matrix.slice(r, r + half).map(row => row.slice(c, c + half))));
21+
};
22+
return quad(arr)
23+
.flat(Infinity)
24+
.reduce((a, c) => (c === 0 ? [a[0] + 1, a[1]] : [a[0], a[1] + 1]), [0, 0]);
25+
}

Diff for: utils/build.js

+7-8
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ const newREADME = `# 프로그래머스 모든 문제 풀이
2222
- ⭐ **도움이 되셨다면** 오른쪽 상단 ↗ 의 ⭐️ **Star를 클릭**해 이 프로젝트를 응원해주세요!
2323
2424
## 👻 [크롬익스텐션](https://chrome.google.com/webstore/detail/%ED%94%84%EB%A1%9C%EA%B7%B8%EB%9E%98%EB%A8%B8%EC%8A%A4-%EC%A0%95%EB%8B%B5-%ED%86%B5%EA%B3%BC%EA%B8%B0/pogpgnlafgchgebcnohihjjmdjcffenl?hl=ko) 이 출시되었습니다 🎉🎉
25-
- 😆 다운로드 👉 [프로그래머스 정답 통과기 - 크롬 웹 스토어](https://chrome.google.com/webstore/detail/%ED%94%84%EB%A1%9C%EA%B7%B8%EB%9E%98%EB%A8%B8%EC%8A%A4-%EC%A0%95%EB%8B%B5-%ED%86%B5%EA%B3%BC%EA%B8%B0/pogpgnlafgchgebcnohihjjmdjcffenl?hl=ko)
26-
- 🎉 출시 이벤트(~ 9/5): 리뷰를 남기신 분들 중 희망하는 분들께 1대1 커피챗(10분 내외, 온라인)을 제공합니다! 리뷰를 남기고 캡쳐와 함께 [email protected] 로 연락주세요! 😁
25+
- 😆 무료 다운로드 👉 [프로그래머스 정답 통과기 - 크롬 웹 스토어](https://chrome.google.com/webstore/detail/%ED%94%84%EB%A1%9C%EA%B7%B8%EB%9E%98%EB%A8%B8%EC%8A%A4-%EC%A0%95%EB%8B%B5-%ED%86%B5%EA%B3%BC%EA%B8%B0/pogpgnlafgchgebcnohihjjmdjcffenl?hl=ko)
2726
2827
[![extensionIntro](https://user-images.githubusercontent.com/54318460/187886360-dd8f917e-4ffe-4c6f-9b3c-d10a69b5f46e.gif)](https://chrome.google.com/webstore/detail/%ED%94%84%EB%A1%9C%EA%B7%B8%EB%9E%98%EB%A8%B8%EC%8A%A4-%EC%A0%95%EB%8B%B5-%ED%86%B5%EA%B3%BC%EA%B8%B0/pogpgnlafgchgebcnohihjjmdjcffenl?hl=ko)
2928
@@ -42,16 +41,16 @@ const newREADME = `# 프로그래머스 모든 문제 풀이
4241
4342
### Level 1 ✅
4443
45-
- 전체 문제 수: 56문제
44+
- 전체 문제 수: 57문제(레벨 변동에 따라 차이가 있을 수 있습니다)
4645
- 풀이 문제 수: ${tables[0].length}문제
4746
4847
| 번호 | 문제 출처 | 풀이 |
4948
| --- | ------- | --- |
5049
${tables[0].join('\n')}
5150
52-
### Level 2 👨🏻‍💻(풀이 중..)
51+
### Level 2
5352
54-
- 전체 문제 수: 65문제
53+
- 전체 문제 수: 72문제(레벨 변동에 따라 차이가 있을 수 있습니다)
5554
- 풀이 문제 수: ${tables[1].length}문제
5655
5756
| 번호 | 문제 출처 | 풀이 |
@@ -60,7 +59,7 @@ ${tables[1].join('\n')}
6059
6160
### Level 3 👨🏻‍💻(풀이 중..)
6261
63-
- 전체 문제 수: 52문제
62+
- 전체 문제 수: 51문제(레벨 변동에 따라 차이가 있을 수 있습니다)
6463
- 풀이 문제 수: ${tables[2].length}문제
6564
6665
| 번호 | 문제 출처 | 풀이 |
@@ -69,7 +68,7 @@ ${tables[2].join('\n')}
6968
7069
### Level 4
7170
72-
- 전체 문제 수: 22문제
71+
- 전체 문제 수: 19문제(레벨 변동에 따라 차이가 있을 수 있습니다)
7372
- 풀이 문제 수: ${tables[3].length}문제
7473
7574
| 번호 | 문제 출처 | 풀이 |
@@ -78,7 +77,7 @@ ${tables[3].join('\n')}
7877
7978
### Level 5
8079
81-
- 전체 문제 수: 6문제
80+
- 전체 문제 수: 6문제(레벨 변동에 따라 차이가 있을 수 있습니다)
8281
- 풀이 문제 수: ${tables[4].length}문제
8382
8483
| 번호 | 문제 출처 | 풀이 |

0 commit comments

Comments
 (0)